I have this simple data set from Excel:
Date Person Amount
Jan-18 jason 1
Jan-18 fred 2
Jan-18 george 3
Feb-18 jason 10
Feb-18 fred 12
Feb-18 george 15
Feb-18 jim 25
I added two measures:
Amount = SUM( Data[Amount] )
and
Average Amount per Person =
AVERAGEX(
VALUES( Data[Person]),
[Amount]
)
This works as I expect and is dynamic when I select a specific Date:
What I now want is "Number of Persons Above Average" - so in the screenshot only Jim is above 15.50 so the measure should return 1.
My attempt at this measure is this:
Number of Persons Above Average =
CALCULATE(
DISTINCTCOUNT( Data[Person] ),
FILTER(
Data,
SUM( Data[Amount] ) >= [Average Amount per Person]
)
)
As you can see below it just returns the number of persons displayed - in this case 4
How do I amend the above measure to the correct DAX ?
I like to use variables in situations like these:
Number of Persons Above Average =
VAR AveragePerPerson = [Average Amount per Person]
RETURN
CALCULATE ( DISTINCTCOUNT ( Data[Person] ),
Data[Amount] >= AveragePerPerson )
This way you don't have to worry about how the average measure will be computed inside of the CALCULATE and you don't have to use a FILTER function.
Related
I have two tables - Customer and Transaction. The transaction holds invoices and payments. I need to get the date when the transaction sum is 0 or grater.
So the expected result should be John 02-20-2021. How can I achieve this?
customerID
Name
1
John
2
Ben
customerID
value
date
1
-150
02-13-2021
1
100
02-14-2021
1
200
02-20-2021
1
10
02-23-2021
You can try this out, by adding some new measures.
First measure to calculate the account balance over dates, returning a value only for positive balance:
Account Balance :=
VAR _date = MAX ( Transaction[date] )
VAR _balance =
CALCULATE (
SUM ( Transaction[value] ) ,
Transaction[date] <= _date
)
RETURN
IF ( _balance >= 0 , _balance )
The second filters the original table based on this measure and returns the earliest date:
Break Even Date :=
MINX (
FILTER (
VALUES ( Transaction[date] ),
[Account Balance]
),
[date]
)
Result:
Do note that I have not tested this beyond your (simple) example.
I have table with Sales. All Sales divide between Men and Women. I need to find out who have the biggest count of sales, men or woman.
I have tried using Summarize and MAXX together, but sonething is wrong.
MAXX(SUMMARIZE(
'public Brand',
'public Brand'[Возрастная группа],
"Свод",
COUNT('public Brand'[Id]))
If I understand your requirement correct, you need a single output "Men" OR "Women" based on the count sales count/amount. For example, if there are total 10 count or row for Men and 12 for Women, you need Women as output from the measure. I prepared a very simple data set for example (Excel part in the image) to calculate the measure. You can see the final output in the red marked box.
Here below is the Measure Code-
For most number of sales number
Gender With Most Count =
VAR Gourp_by_gender_with_count =
SUMMARIZE(
Sales,
Sales[Gender],
"GenderCount", COUNT(Sales[Gender])
)
VAR max_count_among_gender_group =
MAXX (
TOPN(1,Gourp_by_gender_with_count,[GenderCount],DESC),
[Gender]
)
RETURN max_count_among_gender_group
For most number of sales amount
Gender With Most Sales Amount =
VAR Gourp_by_gender_with_amount =
SUMMARIZE(
Sales,
Sales[Gender],
"GenderWiseSales", SUM(Sales[Amount])
)
VAR max_amount_among_gender_group =
MAXX (
TOPN(1,Gourp_by_gender_with_amount,[GenderWiseSales],DESC),
[Gender]
)
RETURN max_amount_among_gender_group
I have 2 tables, the first is a sanitised version of the data, the second is what I need to calculate.
AnimalCount
Animal DATE Count
Lion 2019-01-01 2
Cat 2019-01-01 45
Cat 2019-01-02 40
Fish 2019-01-02 900
Cat 2019-01-03 50
Fish 2019-01-05 800
Fish 2019-01-06 1200
Cat 2019-01-07 45
Lion 2019-01-07 1
Lion 2019-01-08 0
Calculated
DATE (unique) Sum of latest Count by Animal
2019-01-01 47 = 45 Cats + 2 Lions
2019-01-02 942 = 900 Fish + 40 Cats + 2 Lions
2019-01-03 952 = 50 Cats + 900 Fish + 2 Lions
2019-01-05 852 = 800 Fish + 50 Cats + 2 Lions
2019-01-06 1252 = 1200 Fish + 50 Cats + 2 Lions
2019-01-07 1246 = 1 Lion + 45 Cats + 1200 Fish
2019-01-08 1245 = 45 Cats + 1200 Fish
Here are the queries I've tried so far, both of them give slightly different results to what I'm looking for.
I'd be super grateful for any suggestions?
Query 1.
LatestTotal = SUMX(
GROUPBY(
FILTER(
AnimalCount,
[DATE] <= EARLIER([DATE])
),
[Animal],
"LatestGroupTotal",
SUMX(CURRENTGROUP(), [Count])
),
[LatestGroupTotal]
)
Query 2.
LatestTotal = SUMX(
ADDCOLUMNS(
ADDCOLUMNS(
VALUES(AnimalCount[Animal]),
"AnimalName",
[Animal],
"LastDate",
CALCULATE(
MAX(AnimalCount[DATE]),
NOT(ISBLANK(AnimalCount[Count]))
)
),
"LastValue",
SUMX(
FILTER(
AnimalCount,
[AnimalName] = [Animal]
&& [DATE] = [LastDate]
),
[Count]
)
),
[LastValue]
)
First, create a measure for total animal count (for further convenience):
Total Animal Count = SUM(AnimalCount[Count])
Then, add this measure:
Latest Total =
VAR
Current_Date = MAX ( AnimalCount[DATE] )
VAR
All_Latest_Dates =
ADDCOLUMNS (
ALL ( AnimalCount[Animal] ),
"Last Date", CALCULATE (
MAX ( AnimalCount[DATE] ),
AnimalCount[DATE] <= Current_Date )
)
VAR Animal_Latest_Dates =
TREATAS ( All_Latest_Dates, AnimalCount[Animal], AnimalCount[DATE] )
RETURN
CALCULATE ( [Total Animal Count], Animal_Latest_Dates )
Result:
Explanation:
This formula requires an understanding of "data lineage" concept in DAX:
Understanding Data Lineage in DAX
In short:
store date visible in a current context in a variable;
then, generate a virtual table ("All_Latest_Dates"), that consists of all animals, and their last known date for each "current" date;
next, apply this virtual table to the AnimalCount table using TREATAS (please read the article to understand how it works);
finally, calculate animal count using the virtual table as a new filter context.
This approach results in an optimal query execution plan, and the formula should give you good performance even if your data is large.
How about this?
LatestTotal =
VAR CurrDate =
MAX ( AnimalCount[DATE] )
VAR Summary =
SUMMARIZE (
FILTER ( ALLSELECTED ( AnimalCount ), AnimalCount[DATE] <= CurrDate ),
AnimalCount[Animal],
"LastDate", MAX ( AnimalCount[DATE] )
)
VAR CountAtDate =
ADDCOLUMNS (
Summary,
"Count", LOOKUPVALUE (
AnimalCount[Count],
AnimalCount[Animal], [Animal],
AnimalCount[DATE], [LastDate]
)
)
RETURN
SUMX ( CountAtDate, [Count] )
First, we summarize the table by Animal looking for the last date associated with each one that is less than or equal to the date of the row we're in (CurrDate).
Then we look up the count for that animal on that date and add all those counts together.
I have a Measure which calculates a cumulative total:
CumulativeCount:=
VAR date1 = MAX( DimDate[Date] )
VAR date2 = MAX( FactTable[EndDate] )
RETURN
CALCULATE (
SUM( FactTable[Count] ),
DimDate[Date] <= date1,
DimDate[Date] <= date2,
ALL( DimDate[Date] )
)
And another, actually used in the Pivot Table, which, when it's calculating the Grand Total, is supposed to add up the cumulative totals for each date:
CumulativeCountForPivot:=
IF (
-- If calculating for one group
COUNTROWS( VALUES( FactTable[Group] ) ) = 1,
-- Do core logic
[CumulativeCount],
-- Else add up the results from each group
SUMX(
VALUES( FactTable[Group] ),
[CumulativeCount]
)
)
I don't understand why the Grand Total in the final column is 12, not 6.
The reason is that the grand total is for GroupA and GroupB combined and there is a cumulative count of 12 on that date (6 for each group).
On 06/01/2017 there are no records for GroupA so the [CumulativeCount] measure is returning a blank, even though there are records before that date and the count would be 6. If you added a record for GroupA with a Count of 0 on 06/01/2017, then you would see 6 appear.
If you want a measure that only shows 6 on that date, then try something like this:
CountForPivot =
VAR TempTable = SUMMARIZE(FactTable,
FactTable[Group],
FactTable[EndDate],
"Cumulative",
CALCULATE(SUM(FactTable[Count]),
FILTER(ALLEXCEPT(FactTable, FactTable[Group]),
FactTable[EndDate] <= MAX(FactTable[EndDate])
)
)
)
RETURN SUMX(TempTable, [Cumulative])
I have a sheet containing employees leave data-
Staff Leave Taken Month
A 19 April
A 3 May
A 3 June
B 2 April
B 1 May
B 0 June
C 0 April
C 0 May
C 1 June
I want to calculate the Employee whose has taken maximum no. of leaves and name of employee whose has taken minimum no. of leaves.
Here Employee with max. leaves is A and with min. is C.
I am having trouble in getting the maximum no. of leaves.
X = MAX( SUMX ( SUMMARIZE ( Table1, Table1[STAFF], Table1[Leaves] ), [Leaves] ))
But it is showing some error.
I tried to group by staff name then also it is not working out.
You can first create a summary table with the following DAX:
Summary = SUMMARIZE(Table1, Table1[Staff], "Leaves", SUM(Table1[Leave Taken]))
Then you can use the following DAX measure to get the Max / Min name:
Max Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MAX(Summary[Leaves])
)
)
-
Min Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MIN(Summary[Leaves])
)
)