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
mlovejoy
Helper I
Helper I

DAX Measure Sum a column base on Grouping, Max date and Condition

Hello community, 

 

I'm trying in a DAX measures to retrieve the number of Status for each unique key by doing the sum of criteria column.

- Rules are always to keep the Max date for each unique key groups

- Filtering criteria = 1

- if status have the same date and are both "valide" then I need to say keep first Cancelled, then confirmed, then accepted,... to get only 1 status by unique key

 

Any genius here :-)?

 

Regards,

Mark

Screenshot 2022-12-21 at 17.41.45.png

1 ACCEPTED SOLUTION

Hi , @mlovejoy 

Thanks for your quick response and detailed explanation for your need !

According to your description, you want to calculate the [Criteria]=1 and the date is the latest date group ny unique key.And when it have one more status (1st cancelled, 2nd Accepted).

Here are the steps you can refer to :
(1)My test data is the same as yours.

(2)We can create a measure like this:

Measure = var _t1= FILTER(ALLSELECTED('Table'),'Table'[Criteria]=1)
var _latest_date =FILTER( ADDCOLUMNS(_t1 , "latestDate" , var _key = [Unique Key] return MAXX(FILTER(_t1 , 'Table'[Unique Key]=_key ) ,[Date])) , [Date] = [latestDate])
var _t2 = SUMMARIZE(_latest_date,[Unique Key],[Status],[Criteria]) 
var _t3 = ADDCOLUMNS(_t2 , "count" , var _key=[Unique Key] return COUNTROWS(FILTER(_t2,[Unique Key]=_key)))
var _t4 =FILTER( ADDCOLUMNS(_t3, "flag", IF([count] <> 1 && [Status]="Accepted" ,0 ,1)),[flag]=1) 
var _curstatus = MAX('Table'[Status])
return
COUNTROWS(FILTER(_t4,[Status]=_curstatus))

Then we can put it in the visual and we can meet your need, the result is as follows:

vyueyunzhmsft_0-1671701115991.png

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

View solution in original post

5 REPLIES 5
v-yueyunzh-msft
Community Support
Community Support

Hi , @mlovejoy 

Thank you very much for the sample data, and based on your description, I don't quite understand your needs.

For your sample data:

vyueyunzhmsft_0-1671674799426.png

When we filter the [Criteria]=1, the status in [Unique key]=1 has no "Accepted", can you detailed explain the logic in this?

And why [Unique key]=2  , the status = "Cancelled"?Can you describe your calculation process in detail?

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

 

Hi @v-yueyunzh-msft , @tamerj1 ,

sorry my sample wasn't correct,...

The business case is the following:

I have a dataset that show only the current status (acc, conf, cancel,..) but it keeps a date in columns when the status was change (1 column for acceptance date, 1 column for cancelation date. Every row of the dataset has a unique key.

Capture2.png

I have appended all dates in 1 column -> creating duplicates of the unique key. 

Capture.PNG

 

By using time intelligence I can recreate the past and know how many where accepted, confirmed, cancelled at each date (reason I need a measure).

 

Calculation:

-Get the latest date of each grouping of Unique key dynamically based on date slicer

- If a grouping has 2 valid status with same max date then prioritize, 1st cancelled, 2nd Accepted,... (if this criteria is to complexe I can find a turnarround)

- In order to retrieve the number of occurence by status I need to sum the criteria column has it remove dupplicates. (specific to the data)

 

To sumup: I need to dynamically group by unique key and filter everygroup to keep the latest date and sum the criteria column to have the number of occurence per status at a given time.

 

I hope it makes it more clear?

 

Thank for you help and time,

Mark

 

 

Hi , @mlovejoy 

Thanks for your quick response and detailed explanation for your need !

According to your description, you want to calculate the [Criteria]=1 and the date is the latest date group ny unique key.And when it have one more status (1st cancelled, 2nd Accepted).

Here are the steps you can refer to :
(1)My test data is the same as yours.

(2)We can create a measure like this:

Measure = var _t1= FILTER(ALLSELECTED('Table'),'Table'[Criteria]=1)
var _latest_date =FILTER( ADDCOLUMNS(_t1 , "latestDate" , var _key = [Unique Key] return MAXX(FILTER(_t1 , 'Table'[Unique Key]=_key ) ,[Date])) , [Date] = [latestDate])
var _t2 = SUMMARIZE(_latest_date,[Unique Key],[Status],[Criteria]) 
var _t3 = ADDCOLUMNS(_t2 , "count" , var _key=[Unique Key] return COUNTROWS(FILTER(_t2,[Unique Key]=_key)))
var _t4 =FILTER( ADDCOLUMNS(_t3, "flag", IF([count] <> 1 && [Status]="Accepted" ,0 ,1)),[flag]=1) 
var _curstatus = MAX('Table'[Status])
return
COUNTROWS(FILTER(_t4,[Status]=_curstatus))

Then we can put it in the visual and we can meet your need, the result is as follows:

vyueyunzhmsft_0-1671701115991.png

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Hello @v-yueyunzh-msft  , 

thanks for you help and sorry for my late reply, I was not working during he past 2 weeks. 

 

I've tested your solution , however I see 2 problems at the end of the formula:

 

1st: var _t4 = FILTER(ADDCOLUMNS(_t3 , "flag" , IF([count] <> 1 && 'Test Append Tables'[Status] ="Accepted" , 0 , 1)) , [flag] =1) // this variable excludes only "Accepted" status, however we can have 2 or more duplicates with different status. -> It means there needs to be a hierachy of selection. If duplicates than first keep Cancelled, than Deferred than Accepted. I would imagine a combination of Switch(true(), to create the hierarchy and MAX or MIn to selected the correct status in the hierarchy.

Capture6.PNG

 

2nd: In a Matrice the Total doesn't work. Any ideas why?

 

Thanks again for your help, this time I will be ready to accept your solution 🙂

 

Regards,

Mark

tamerj1
Super User
Super User

Hi @mlovejoy 

please double check the expected results as they seem not to match the description (at least for key 1 and key 2)

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

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.