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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
kalkhudary
Helper IV
Helper IV

Logic 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. However, I need to batch based on Date and NOT Batched Date as this field is not reliable.

 

I will need the dax to generate the columns colored in purple

DatePB-NumberName Batched Date Batch Name  Rank Current Batch ?
10-02-2023PB-1003EFT - PB-1003-10/02/202310/2/2023 17:25 P.M.Batch 11No
10-02-2023PB-1004Cheque - PB-1004-10/02/202310/2/2023 17:08 P.M.Batch 11No
10-23-2023PB-1005EFT - PB-1005-10/23/202310/23/2023 17:25 P.MBatch 22No
10-25-2023PB-1006Cheque - PB-1006-10/25/202310/25/2023 17:00 P.M.Batch 22No
10-26-2023PB-1007EFT - PB-1007-10/26/202310/26/2023 16:15 P.M.Batch 33Yes
10-27-2023PB-1008Cheque - PB-1008-10/27/202310/27/2023 15:00 P.M.Batch 33Yes

 

This is what is showing in the results of the Daxes. The Next batch date is not reflecting or the 1st row correctly, It should be 10/02/2023 and for the previous batch date The second row should reflect 10/02/2023. Days diff results is giving me wrong calculations so the rank logic is turning wrong too. 

I am stuck as I really need the logics method for my report generation.

Batch 1.PNG

A community person helped me Dax the below but I may be doing something wrong that I am not getting the results right.

 

Next batch date = CALCULATE(Min(Sheet1[Created On]),FILTER(Sheet1,Sheet1[Created On]>EARLIER(Sheet1[Created On])&&LEFT(Sheet1[Name],6)="Cheque"))
 
Previous batch date = CALCULATE(MAX(Sheet1[Created On]),FILTER(Sheet1,Sheet1[Created On]<EARLIER(Sheet1[Created On])&&LEFT(Sheet1[Name],3)="EFT"))
 
Days diff = if(LEFT(Sheet1[Name],3)="EFT",1*(Sheet1[Created On]-[Previous batch date]),1*([Next batch date]-Sheet1[Created On]))
 
Rank = if([Days diff]<=7,CALCULATE(COUNTROWS(Sheet1),FILTER(Sheet1,Sheet1[Batched Date]<=EARLIER(Sheet1[Batched Date])&&Sheet1[Name]=EARLIER(Sheet1[Name]))),BLANK())

 

 

1 ACCEPTED SOLUTION
sevenhills
Super User
Super User

I cannot match your expected data as weeknum is giving the same for your rows 2 and above.

I created similar dataset and able to get these results 

Using Dax Columns:

 

Rank Column = RANKX ( 'Table', WEEKNUM('Table'[Date]),,ASC, Dense)

Batch Name Column = "Batch " & [Rank Column]

Current Batch Column = If ( [Rank Column] = MAX (  'Table'[Rank Column]  ) , "Yes", "No")

sevenhills_0-1699045381521.png

I think it will be good to have measures for this scenario, so providing the syntax for that also:

Rank Measure = RANKX( allselected('Table'), WEEKNUM(  CALCULATE( max('Table'[Date]))) ,,asc, dense)

Batch Name Measure = "Batch " & [Rank Measure]



Current Batch Measure = 
var _Max = MAXX ( all('Table'),  [Rank Measure] )
RETURN If ( [Rank Measure] = _Max , "Yes", "No")

 

sevenhills_1-1699045526929.png

 

If the source data and your expectation is per your post, then adjust to your needs the weeknum portion of DAX. 

 

Hope it helps!

View solution in original post

1 REPLY 1
sevenhills
Super User
Super User

I cannot match your expected data as weeknum is giving the same for your rows 2 and above.

I created similar dataset and able to get these results 

Using Dax Columns:

 

Rank Column = RANKX ( 'Table', WEEKNUM('Table'[Date]),,ASC, Dense)

Batch Name Column = "Batch " & [Rank Column]

Current Batch Column = If ( [Rank Column] = MAX (  'Table'[Rank Column]  ) , "Yes", "No")

sevenhills_0-1699045381521.png

I think it will be good to have measures for this scenario, so providing the syntax for that also:

Rank Measure = RANKX( allselected('Table'), WEEKNUM(  CALCULATE( max('Table'[Date]))) ,,asc, dense)

Batch Name Measure = "Batch " & [Rank Measure]



Current Batch Measure = 
var _Max = MAXX ( all('Table'),  [Rank Measure] )
RETURN If ( [Rank Measure] = _Max , "Yes", "No")

 

sevenhills_1-1699045526929.png

 

If the source data and your expectation is per your post, then adjust to your needs the weeknum portion of DAX. 

 

Hope it helps!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.