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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kalkhudary
Helper III
Helper III

Dax to group rows as one batch based on created date and Rank

Hello Community, 

 

I am trying to find a solution for a scenrio where I need to dax couple of columns (1) which will help me group batches based on the created on date of the batch where for any batch that falls within a week of the earlier created on date, it will be grouped as one batch and will be named batch 1 and to identify that the upcoming one will be batch 2 then 3 then 4 etc....

 

This is the data I have. For example PB-1003 and PB-1004 will be Batch 1 as the rule applies that they fall in same week starting from Oct 2  and PB-1005 and PB-1006 will be batch 2 as the rule applies that they fall within the same week starting from 16 Oct and PB-1007 and PB-1008 will be batch 3 as they fall within a week starting October 24.

 

Then I need a second column to rank the batches.  Then a third column that identifies if the batch is the current or not based on the last issued batch

 

I will need the dax to generate this column colored in purple

DatePB-NumberName Batched Date Batch Name  Rank Current Batch ?
2-Oct-23PB-1003EFT - PB-1003-10/02/202310/2/2023 10:15 a.m.Batch 11No
2-Oct-23PB-1004Cheque - PB-1004-10/05/202310/5/2023 9:15 a.m.Batch 11No
16-Oct-23PB-1005EFT - PB-1005-10/16/202310/16/2023 10:15 a.m.Batch 22No
21-Oct-23PB-1006Cheque - PB-1006-10/21/202310/21/2023 9:15 a.m.Batch 22No
24-Oct-23PB-1007EFT - PB-1007-10/24/202310/24/2023 10:15 a.m.Batch 33Yes
27-Oct-23PB-1008Cheque - PB-1008-10/27/202310/27/2023 9:15 a.m.Batch 33Yes

 

I tried multiple ways but still failing to get the right outcome. Any help will be appreciated.

3 ACCEPTED SOLUTIONS
Ashish_Mathur
Super User
Super User

Hi,

Try these calculated column formulas

Next batch date = CALCULATE(MIN(Data[Batched Date]),FILTER(Data,Data[Batched Date]>EARLIER(Data[Batched Date])&&LEFT(Data[Name],6)="Cheque"))
Previous batch date = CALCULATE(max(Data[Batched Date]),FILTER(Data,Data[Batched Date]<EARLIER(Data[Batched Date])&&LEFT(Data[Name],3)="EFT"))
Days diff = if(LEFT(Data[Name],3)="EFT",1*([Next batch date]-[Batched Date]),1*([Batched Date]-[Previous batch date]))
Type = LEFT(Data[Name],SEARCH(" ",Data[Name])-1)
Rank = if([Days diff]<=7,CALCULATE(COUNTROWS(Data),FILTER(Data,Data[Batched Date]<=EARLIER(Data[Batched Date])&&Data[Type]=EARLIER(Data[Type]))),BLANK())
Batch name = "Batch "&Data[Rank]

Hope this helps.

Ashish_Mathur_0-1698636723054.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

Ahmedx
Super User
Super User

10 REPLIES 10
kalkhudary
Helper III
Helper III

@Ashish_Mathur The solution of Ahmed did work but I was looking for a dax logic for the solution more than a table to connect to. If you had a chance to check why the logics are not working if we are basing them on the Date rather than the Batched date. So curious to learn the reason, what's wrong in this context.

Think over the logic for the final time and state that very clearly.  Share the download link of the PBI file and show the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

pls try yhis

 

@Ahmedx The variables worked great! Thanks for the quick turnaround.

kalkhudary
Helper III
Helper III

@Ashish_Mathur I just realized something when setting the logics. It is somehow not giving me the right difference. I am using the Date instead of Batched date as the batched date field might not be reflected on time. 

 

I have used the same logics that you posted. Any idea why the date diff is reflecting wrong.

 

 

Batching.PNG

 

Batching-DateDiff.PNGhttps://app.powerbi.com/groups/me/reports/4d79d3cf-2b0b-41f6-a6d7-35dc8490fa10/ReportSection?experie... 

Try the solution posted by the other contributor.  Does that work?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
kalkhudary
Helper III
Helper III

@Ashish_Mathur Thank you so much for this solution. I tried it and It worked.

 

@Ahmedx I also tried your method and It works Super! Thanks, you made my day 🙂

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ahmedx
Super User
Super User

try this

Screenshot_5.png

Ashish_Mathur
Super User
Super User

Hi,

Try these calculated column formulas

Next batch date = CALCULATE(MIN(Data[Batched Date]),FILTER(Data,Data[Batched Date]>EARLIER(Data[Batched Date])&&LEFT(Data[Name],6)="Cheque"))
Previous batch date = CALCULATE(max(Data[Batched Date]),FILTER(Data,Data[Batched Date]<EARLIER(Data[Batched Date])&&LEFT(Data[Name],3)="EFT"))
Days diff = if(LEFT(Data[Name],3)="EFT",1*([Next batch date]-[Batched Date]),1*([Batched Date]-[Previous batch date]))
Type = LEFT(Data[Name],SEARCH(" ",Data[Name])-1)
Rank = if([Days diff]<=7,CALCULATE(COUNTROWS(Data),FILTER(Data,Data[Batched Date]<=EARLIER(Data[Batched Date])&&Data[Type]=EARLIER(Data[Type]))),BLANK())
Batch name = "Batch "&Data[Rank]

Hope this helps.

Ashish_Mathur_0-1698636723054.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.