Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
whjqb
Frequent Visitor

Calculate values at different granular in DAX measure

I am struggling on creating a DAX measure to solve below problem. 

 

I would like to filter on below dataset where dim_reporting_date_key is the latest date for each dim_case_key with a quarter, then counting how many cases at "Age Over Time" level. 

 

I only show partially data here as the full dataset is big. 

whjqb_2-1731537492138.png

 

 

The script should like below if I use SQL.

 

select [Age Over Time],
       count(distinct dim_case_key) as result
from
(
    select dim_case_key,
           dim_reporting_date_key,
           [Case Status],
           [Open Days],
           [Age Over Time],
           max(dim_reporting_date_key) OVER (PARTITION BY dim_case_key) as LatestDate
    from #temp2
) x
where dim_reporting_date_key = [LatestDate]
group by [Age Over Time]

 

 

Here is my dax measure.

 

// DAX Query
DEFINE
	VAR __Filter =
	TREATAS(
		{
			20240731,
			20240831,
			20240930
		},
		'Fact Case Monthly Snapshot'[dim_reporting_date_key]
	)

	MEASURE 'Fact Case Monthly Snapshot'[LDate] = (
		CALCULATE(
			MAX('Fact Case Monthly Snapshot'[dim_reporting_date_key]),
			ALLSELECTED(
				'Fact Case Monthly Snapshot'[dim_reporting_date_key],
				'Fact Case Monthly Snapshot'[Age Over Time]
			)
		)
		)
	MEASURE 'Fact Case Monthly Snapshot'[Result] =

		VAR latestDate =
		CALCULATE(
			MAX('Fact Case Monthly Snapshot'[dim_reporting_date_key]),
			ALLSELECTED(
				'Fact Case Monthly Snapshot'[dim_reporting_date_key],
				'Fact Case Monthly Snapshot'[Age Over Time]
			)
		)
		VAR latestValue =
		CALCULATE(
			DISTINCTCOUNT('Fact Case Monthly Snapshot'[dim_case_key]),
			FILTER(
				'Fact Case Monthly Snapshot',
				'Fact Case Monthly Snapshot'[dim_reporting_date_key] = latestDate
			)
		)

		RETURN
			SUMX(
				SUMMARIZE(
					'Fact Case Monthly Snapshot',
					'Fact Case Monthly Snapshot'[dim_case_key],
					"LatestValue", latestValue
				),
				[LatestValue]
			)
			


	VAR __DS0Core =
	SUMMARIZECOLUMNS(
		'Fact Case Monthly Snapshot'[dim_case_key],
		'Fact Case Monthly Snapshot'[dim_reporting_date_key],
		'Fact Case Monthly Snapshot'[Age Over Time],
		__Filter,
		"Result", 'Fact Case Monthly Snapshot'[Result],
		"Latest Date", [LDate]
	)

	VAR __DS0Core2 =
	SUMMARIZECOLUMNS(
		'Fact Case Monthly Snapshot'[Age Over Time],
		__Filter,
		"Result", 'Fact Case Monthly Snapshot'[Result],
		"Latest Date", [LDate]
	)

EVALUATE

	// __DS0Core
	// ORDER BY
	// 	'Fact Case Monthly Snapshot'[dim_case_key],
	// 	'Fact Case Monthly Snapshot'[dim_reporting_date_key] DESC


	__DS0Core2

 

The result is incorrect. The value of result is too high.

 

whjqb_3-1731537966073.png

 

If I run the measure at case granular and the result looks good. 

 

// DAX Query
DEFINE
	VAR __Filter =
	TREATAS(
		{
			20240731,
			20240831,
			20240930
		},
		'Fact Case Monthly Snapshot'[dim_reporting_date_key]
	)

	MEASURE 'Fact Case Monthly Snapshot'[LDate] = (
		CALCULATE(
			MAX('Fact Case Monthly Snapshot'[dim_reporting_date_key]),
			ALLSELECTED(
				'Fact Case Monthly Snapshot'[dim_reporting_date_key],
				'Fact Case Monthly Snapshot'[Age Over Time]
			)
		)
		)
	MEASURE 'Fact Case Monthly Snapshot'[Result] =

		VAR latestDate =
		CALCULATE(
			MAX('Fact Case Monthly Snapshot'[dim_reporting_date_key]),
			ALLSELECTED(
				'Fact Case Monthly Snapshot'[dim_reporting_date_key],
				'Fact Case Monthly Snapshot'[Age Over Time]
			)
		)
		VAR latestValue =
		CALCULATE(
			DISTINCTCOUNT('Fact Case Monthly Snapshot'[dim_case_key]),
			FILTER(
				'Fact Case Monthly Snapshot',
				'Fact Case Monthly Snapshot'[dim_reporting_date_key] = latestDate
			)
		)

		RETURN
			SUMX(
				SUMMARIZE(
					'Fact Case Monthly Snapshot',
					'Fact Case Monthly Snapshot'[dim_case_key],
					"LatestValue", latestValue
				),
				[LatestValue]
			)
			


	VAR __DS0Core =
	SUMMARIZECOLUMNS(
		'Fact Case Monthly Snapshot'[dim_case_key],
		'Fact Case Monthly Snapshot'[dim_reporting_date_key],
		'Fact Case Monthly Snapshot'[Age Over Time],
		__Filter,
		"Result", 'Fact Case Monthly Snapshot'[Result],
		"Latest Date", [LDate]
	)

	VAR __DS0Core2 =
	SUMMARIZECOLUMNS(
		'Fact Case Monthly Snapshot'[Age Over Time],
		__Filter,
		"Result", 'Fact Case Monthly Snapshot'[Result],
		"Latest Date", [LDate]
	)

EVALUATE

	__DS0Core
	ORDER BY
	'Fact Case Monthly Snapshot'[dim_case_key],
	'Fact Case Monthly Snapshot'[dim_reporting_date_key] DESC


	// __DS0Core2

 

The result shows 1 for the correct records and blank for others.

 

whjqb_4-1731538167485.png

 

I am not sure where is wrong of my measure and why the result is aggregated incorrectly. Can someone help me to solve this issue please?

 

Thanks a lot

 

 

6 REPLIES 6
v-xuxinyi-msft
Community Support
Community Support

Hi @whjqb 

 

Thanks for the reply from Poojara_D12 .

 

I have created the following measure to calculate LatestDate,

LatestDate = CALCULATE(MAX([dim_reporting_date_key]), 'Fact Case Monthly Snapshot'[dim_case_key] = MAX([dim_case_key]))

 

vxuxinyimsft_0-1731571155867.png

 

but I have some questions about the result of count. What is the calculation logic of count? Is it to get the maximum date of each Age Over time, and if there is a maximum date of dim_case_key that is the same as it, then count it? Could you please give the expected result based on the sample data you provided so that we can better help you?

 

Best Regards,
Yulia Xu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Yulia,

Thanks for your help. Here is more explanation. 

 

"max_date" is the maximum dim_reporting_date_key for each dim_case_key. "Selected" is Y if dim_reporting_date_key = max_date. I would like to select those "Y" records and ignore others.

whjqb_1-1731618322473.png

 

 

Then I would like to aggregate to Age Over Time level and count distinct number of  dim_case_key. So the expected result should be like this.

whjqb_2-1731618823185.png

 

Thanks

Ming

 

 

 

Hi @whjqb 

 

I would like to apologize for the belated reply.

 

Please try the following measures:

max_date = CALCULATE(MAX([dim_reporting_date_key]), ALLEXCEPT('Table', 'Table'[dim_case_key]))

 

Selected = IF(MAX([dim_reporting_date_key]) = [max_date], "Y", "NULL")

 

result = CALCULATE(DISTINCTCOUNT('Table'[dim_case_key]), FILTER(ALLEXCEPT('Table', 'Table'[Age Over Time]), [Selected] = "Y"))

 

Output:

vxuxinyimsft_0-1732698538947.png

 

But the result I get is different from yours, according to the screenshot you provided, the result of "60-90" should be 7, not 11. But I think it's because the sample data is incomplete. If you have any questions, please feel free to let me know.

 

Best Regards,
Yulia Xu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Yulia,

Thanks for your response. However, your solution does not work for me. 

 

I also need to add slicer for dim_reporting_date_key to let user dynamic select months. If I select July and Aug, then the max date should be 20240831 for dim_case_key 101388. I tried to add keepfilters in the measures but it is still giving me wrong number.

 

whjqb_0-1732739966255.png

 

Poojara_D12
Memorable Member
Memorable Member

Hi @whjqb 

It seems the issue you're facing is due to an incorrect aggregation, which results in higher values than expected. This can happen if the calculation isn’t properly limited to the latest date per dim_case_key. In your case, the key is to ensure that you’re only counting each dim_case_key once at its latest reporting date.

Here’s an approach to modify your DAX to make sure you only count each case on the latest reporting date:

Solution

  1. Identify the Latest Date Per Case: Create a variable to capture the latest date per dim_case_key.
  2. Filter for Latest Date: Use this variable in a filter to make sure only cases with their latest date are counted.
  3. Count Distinct Cases: Finally, count the distinct cases based on the filtered table.

Here’s how this DAX measure could look:

 

Result = 
VAR LatestDatePerCase =
    CALCULATE(
        MAX('Fact Case Monthly Snapshot'[dim_reporting_date_key]),
        ALLEXCEPT('Fact Case Monthly Snapshot', 'Fact Case Monthly Snapshot'[dim_case_key])
    )

RETURN
    CALCULATE(
        DISTINCTCOUNT('Fact Case Monthly Snapshot'[dim_case_key]),
        'Fact Case Monthly Snapshot'[dim_reporting_date_key] = LatestDatePerCase,
        TREATAS(
            {20240731, 20240831, 20240930},
            'Fact Case Monthly Snapshot'[dim_reporting_date_key]
        )
    )

 

Explanation

  1. LatestDatePerCase: This variable calculates the latest dim_reporting_date_key for each dim_case_key, ignoring other filters.
  2. DISTINCTCOUNT with Filter: We then use DISTINCTCOUNT to count distinct cases only where dim_reporting_date_key matches LatestDatePerCase for each dim_case_key. The TREATAS function is used to apply the filter to include only relevant dates.

Additional Tips

  • Avoid Nested Aggregation Issues: Summing up DISTINCTCOUNT results can sometimes lead to inflated counts, so the approach here directly applies the date filter within the calculation.
  • Debugging with Intermediate Tables: If you're still having trouble, try using SUMMARIZE or ADDCOLUMNS to create intermediate tables for debugging and verify the date filtering step-by-step.

This should ensure that your measure correctly counts each dim_case_key only once on its latest date. Let me know if this provides the correct result or if further adjustments are needed!

 

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
YouTube: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS

Hi Poojara,

 

Thanks for your help.

 

I also want to aggregate the filtered dataset to Age Over Time level and count the distinct numbers of dim_case_key. More explanations are in my reply to @v-xuxinyi-msft .

 

Thanks

Ming

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.