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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
blytonpereira
Helper II
Helper II

PowerQuery Calculation based on Min value

Hello,

 

I have the following column in my data - Date

 

I need help to add a second column named "Lag Check" that is based on the Min month number from the date column.

Eg if the earliest month number is 09 from the list of Dates, then month number 10 will be "Lag 1 and so on.

The formulas in the Lag Check column should also work when years are changing as show in the example below.

 

Date (Format MM/DD/YYYY)       Lag Check       

 

09/01/2018                                    

09/01/2018

10/01/2018                                    Lag 1          

11/01/2018                                    Lag 2              

11/01/2018                                    Lag 2               

12/01/2018                                    Lag 3               

01/01/2019                                    Lag 4               

01/01/2019                                    Lag 4            

 

I have attached my sample data here. The name of my query is "MI_APR".

https://drive.google.com/open?id=19dqg77i_PItkQG287ePDLE9GxPrwH_eN

 

Thank you.   

1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

Hi @blytonpereira

 

Please see the attached file

 

Basically the steps are

 

1) Sort the Query by Date (Asc Order)
2) Group the Query by Date

3) Add an Index Column

4) Add "lag' as prefix to the Index Column

 

 

 

 

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

 

Download my PBI file from here.

 

Hope this helps.


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

Hi @blytonpereira

 

Please see the attached file

 

Basically the steps are

 

1) Sort the Query by Date (Asc Order)
2) Group the Query by Date

3) Add an Index Column

4) Add "lag' as prefix to the Index Column

 

 

 

 

Hi @Zubair_Muhammad thank you, your file works perfectly.

 

I have another problem though. My actual dataset is much larger and is mainly the refresh is extremely slow and actually doesnt even finish, it is  the Grouping part in PowerQuery that is problematic and slow.

 

Can you pls assist with writing a small R script for this part ? or an alternative method that does not crash.

 

I attached the file here that just hangs due to data volume. Basically the query is refreshing, but stops at some point and doesnt move forward.

https://drive.google.com/open?id=1gyPL3-BALBqMWX1Mu4KasypS46nk7zUV

 

The query is named "STAT_APR"

 

Thank you

@blytonpereira

 

I believe the sorting in Power Query is slowing down the Query

 

If you can do with a calculated column it would be faster

I removed the sort by date Step and used this simple calculated column and it works fast on my PC

 

Please see attached file

 

LAG =
"LAG "
    & RANKX ( STAT_APR, [Date],, ASC, DENSE ) - 1

rankxy.png

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.