Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
I have a datamodel, modelled as a star schema with facts in the middle and dimensions around it. All dimensions are 1 and the facts are on the many side.
The data consists of Checks done on equipment at locations worldwide.
Now, I need a DAX measure to show me the Number of Recent checks. "Recent" in this case, is the last check per combination of [Location]/[EquipmentCategory], these fields are in different dimension tables.
I have an [Index] field in the DimChecks table, that is sorted in a way that a 1 always shows the latest check per [Location]/[EquipmentCategory]. This is modelled upstream in the data flow and outside of PBI/DAX.
So, When nothing is selected in slicers, the desired measure is simple: All Checks where IndexField=1. Simply:
LatestReviews =
CALCULATE(
DISTINCTCOUNT(Facts[CheckID]),
DimChecks[Check Sort Index] =1
)
However, If the user than slices for example, the year 2023, I would like it to show the number of "lastest checks" in that year.
And, for some combinations of [Location]/[EquipmentCategory] the lowest index ín the year 2023, might be 2, if the last check was in 2024.
Obviously, in the above DAX only Checks with [Check Sort Index]=1 are shown.
I think I need something that first aggregates the data and calculates the lowest index number per [Location]/[EquipmentCategory], and that is still affected by all slicers / selections (Year above is an example, there are also other applicable slicers) and then something that counts or sums the values in that table. But I cant seem to get the right formula...
I tried the following, but when sliced to 2023, it only gives me the number of checks where index=1...
LatestChecks =
VAR MinCheck=
CALCULATE(
MIN(DimChecks[Check Sort Index]),
ALLEXCEPT(Facts, DimChecks[EquipmentCategory],DimLocations[LocationName])
)
RETURN
CALCULATE(
DISTINCTCOUNT(Facts[CheckID]),
FILTER(
DimChecks,
DimChecks[Check Sort Index]=MinCheck
))
Hope y'all can help!
Solved! Go to Solution.
Hi @BitKing123
It sounds like the INDEX function would work well for this.
INDEX can be used in this case to return the combinations of EquipmentCategory/LocationName/Check Sort Index corresponding to the minimum Check Sort Index, partitioned by EquipmentCategory/LocationName.
For example:
LatestChecks =
VAR MinCheckCombinations =
INDEX (
1,
SUMMARIZE (
Facts,
DimChecks[EquipmentCategory],
DimLocations[LocationName],
DimChecks[Check Sort Index]
),
ORDERBY ( DimChecks[Check Sort Index], ASC ),
KEEP,
PARTITIONBY (
DimChecks[EquipmentCategory],
DimLocations[LocationName]
)
)
RETURN
CALCULATE (
DISTINCTCOUNT ( Facts[CheckID] ),
MinCheckCombinations
)
Does this work as intended?
Regards
You're welcome @BitKing123
INDEX is one of the relatively new "window functions" introduced in December 2022.
Regarding KEEP:
An alternative (and still perfectly valid) way to write the above measure that I would have suggested before the window functions were released :
LatestChecks =
VAR MinCheckCombinations =
GENERATE (
SUMMARIZE (
Facts,
DimChecks[EquipmentCategory],
DimLocations[LocationName]
),
CALCULATETABLE (
FIRSTNONBLANK ( SUMMARIZE ( Sales, DimChecks[Check Sort Index] ), 0 )
)
)
RETURN
CALCULATE (
DISTINCTCOUNT ( Facts[CheckID] ),
MinCheckCombinations
)
Regards
Hi @BitKing123
It sounds like the INDEX function would work well for this.
INDEX can be used in this case to return the combinations of EquipmentCategory/LocationName/Check Sort Index corresponding to the minimum Check Sort Index, partitioned by EquipmentCategory/LocationName.
For example:
LatestChecks =
VAR MinCheckCombinations =
INDEX (
1,
SUMMARIZE (
Facts,
DimChecks[EquipmentCategory],
DimLocations[LocationName],
DimChecks[Check Sort Index]
),
ORDERBY ( DimChecks[Check Sort Index], ASC ),
KEEP,
PARTITIONBY (
DimChecks[EquipmentCategory],
DimLocations[LocationName]
)
)
RETURN
CALCULATE (
DISTINCTCOUNT ( Facts[CheckID] ),
MinCheckCombinations
)
Does this work as intended?
Regards
@OwenAugerthank you! This is a very interesting approach! In all my Googles, I did not stumble upon this approach 👍
One question on the code: Could you clarify what you intend with KEEP(). DAX does not recognize it (PBI Desktop 2024-04) and I do not think a KEEPFILTERS makes sense here?
It seems to work as intended without KEEP, but I am still curious to know what your intention was here.
You're welcome @BitKing123
INDEX is one of the relatively new "window functions" introduced in December 2022.
Regarding KEEP:
An alternative (and still perfectly valid) way to write the above measure that I would have suggested before the window functions were released :
LatestChecks =
VAR MinCheckCombinations =
GENERATE (
SUMMARIZE (
Facts,
DimChecks[EquipmentCategory],
DimLocations[LocationName]
),
CALCULATETABLE (
FIRSTNONBLANK ( SUMMARIZE ( Sales, DimChecks[Check Sort Index] ), 0 )
)
)
RETURN
CALCULATE (
DISTINCTCOUNT ( Facts[CheckID] ),
MinCheckCombinations
)
Regards
Clear, thank you for the addition explanation!
This alternative approach works as well, same results.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
15 | |
11 | |
8 | |
8 | |
8 |
User | Count |
---|---|
22 | |
13 | |
11 | |
10 | |
10 |