Average Of Column Counting Duplicates Once - PowerPivot + DAX - duplicates

I have a column in PowerPivot that I would like to get the average of. However I only want the rows included that are the only instance of a value or the first instance of a duplicate value in another column. Is this Possible with DAX?
Simply put I need the column average of unique rows, determining uniqueness from another column.

Probably to old to assist, but for those that stumble across:
You would need to create two measures. The first would sum whatever it is you are trying to average by the distinct values in the other column.
| id | squilla |
| 01 | 100 |
| 01 | 110 |
| 02 | 90 |
| 03 | 100 |
| 03 | 90 |
So id=1 has total squilla of 210, id=2 spend of 90, and id=3 spend of 190. The distinct average (where id is the identifier) is 163.333
To do this in powerpivot, first create a measure that sums the squilla by id: Measure1:=CALCULATE(SUM('yourTable'[squilla]),VALUES('yourTable'[id]))
And the second to average it across id:
Measure2:=AVERAGEX(DISTINCT('yourTable'[id]),[Measure1])

My understanding of the OP's question looks something like this:
| id | age |
| -- | --- |
| 1 | 20 |
| 1 | 20 |
| 2 | 50 |
| 3 | 35 |
| 3 | 35 |
In this case, a summed average as suggested by aesthetic_a (40 + 50 + 70)/3 would not be appropriate.
However an averaged average ((40/2) + (50/1) + (35/2))/3 would be a solution to determine the distinct average grouped by id.
Measure:=AVERAGEX(VALUES(table[id]), CALCULATE(AVERAGE(table[age])))

Related

laravel group by date in join query to find sum of values

I am looking for laravel developer to solve a simple issue. I have 3 tables that I am joining to get data. Model data is like this:
date | order number | amount
I need to group by date and find the sum of amount. Like this:
date | order number | amount
12/06/2022 | ask20 | 150
12/06/2022 | ask20 | 50
13/06/2022 | ask21 | 120
15/06/2022 | ask20 | 110
15/06/2022 | ask23 | 10
16/06/2022 | ask20 | 30
Now, I need to group by date to get the value like this:
date | order number | amount
12/06/2022 | ask20 | 200 (added value)
13/06/2022 | ask21 | 120
15/06/2022 | ask20 | 110 (not added as the order number is different)
15/06/2022 | ask23 | 10
16/06/2022 | ask20 | 30
Remember, I am getting this data by joining 3 tables, Can anyone help solve this?
This seems a simple SUM function -
SELECT date, order_number, SUM(amount)
FROM <YOUR BIGGER QUERY..>
GROUP BY date, order_number

Sum a column or another based on a helper column - SQL

I'm using Google Data Studio, and have 12 columns, 1 per month, with numbers, and another column with dates. I'd like to SUM all the numbers that will fall inside a date range based on the date column.
So I've something like this:
+---------+---------+---------+---------+
| DATE | January | February | March |
+---------+---------+---------+---------+
|20180101 | 500 | | |
|20180203 | 150 | | |
|20180201 | | 100 | |
|20180301 | | | 200 |
+---------+---------+---------+---------+
I'd like to have 650 as the result from January extraction, but can't find a solution yet.
Are you looking for aggregation?
select sum(january)
from t
where date >= :datestart and date < :dateend

Compute rank and store it in the same table using SQL while inserting

I have a table of exam result. as follows.
ID | Score | Rank
-------------------------
5 | 34 | 2
13 | 47 | 1
and so on.
I need to compute the rank of a particular ID and save it at the time of insertion of the row to the table. At the same time, the corresponding changes to other rows must also reflect.
So after inserting a new row it should be like
ID | Score | Rank
-------------------------
5 | 34 | 3
13 | 47 | 1
15 | 39 | 2
We can calculate the rank using RANK() function of SQL.
But I can't update it to the same table.
Thanks for any help, in advance.

Properly SQL query

I need to skip results with high price per day. I've got a table like this:
+------+-------------+-------+
| days | return_date | value |
+------+-------------+-------+
| 2 | 2017-12-27 | 15180 |
| 3 | 2017-12-28 | 14449 |
| 4 | 2017-12-29 | 13081 |
| 5 | 2017-12-30 | 11203 |
| 6 | 2017-12-31 | 9497 |
| 6 | 2017-12-31 | 9442 |
+------+-------------+-------+
How can I print only the lowest price for 6 days (9442 in this example).
We can use a GROUP BY clause and an aggregate function. For example:
SELECT t.days
, t.return_date
, MIN(t.value) AS min_value
FROM mytable t
GROUP
BY t.days
, t.return_date
This doesn't really "skip" rows. It accesses all the rows that satisfy the conditions in the WHERE clause (in this example, every row in the table). Then MySQL collapses rows into groups (in this example, rows with identical values of days and return_date get put into a group. The MIN(t.value) aggregate function selects out the minimum (lowest) value out of the group.
The query above is just an example of one approach of satisfying a particular specification.

SQL - select x entries within a timespan

I'm creating a database (in MySQL) with a table of measurements. For each measurement I want to store the DateTime it came in. For showing plots within an app for different intervals (measurements of the day/week/month/year) I want sample the data points I have, so I can return e. g. 30 data points for the whole year as well as for the day/hour. This is the same as done with stock price graphs:
stock price plot for 1 day
vs
stock price plot for 1 month
As you can see, the amount of data points is the same in both pictures.
So how can I select x entries within a timespan in MySQL via SQL?
My data looks like this:
+====+====================+=============+==========+
| id | datetime | temperature | humidity |
+====+====================+=============+==========+
| 1 | 1-15-2016 00:30:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 2 | 1-15-2016 00:35:00 | 19 | 41 |
+----+--------------------+-------------+----------+
| 3 | 1-15-2016 00:40:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 4 | 1-15-2016 00:45:00 | 20 | 42 |
+----+--------------------+-------------+----------+
| 5 | 1-15-2016 00:50:00 | 21 | 42 |
+----+--------------------+-------------+----------+
| 6 | 1-15-2016 00:55:00 | 20 | 43 |
+----+--------------------+-------------+----------+
| 7 | 1-15-2016 01:00:00 | 21 | 43 |
+====+====================+=============+==========+
Let's say, I always want two data points (in reality a lot more). So for the last half hour I want the database to return data point 1 and 4, for the last ten minutes I want it to return 6 and 7.
Thanks for helping!
PS: I'm sorry for any errors in my English
OK, assuming a very simple systematic approach, you can get the first and last entry for any defined period:
select *
from table
where mydatetime =
(select
max(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
OR mydatetime =
(select
min(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
I believe your answer can be found at the following location:
https://stackoverflow.com/a/1891796/7176046
If you are looking to filter out any items not within your date/time your query would use:
Select * from table where Date/Time is (What you want to sort by)