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
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

 

 

 

 


Regards
Zubair

Please try my custom visuals

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

 

 

 

 


Regards
Zubair

Please try my custom visuals

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


Regards
Zubair

Please try my custom visuals

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.