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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
danakajoel
Frequent Visitor

Percentage Difference reference previous Date and Attribute

Hi Community.

 

I have a data set where I am trying to calculate the percentage change between current date and the previous date for different types of stocks in M Query. Is this possible please?

 

Here's an example where I've added a Percent column in Excel where the first percentage change -0.73% is 2693.637/2673.853 - 1

 

i.e. at row level, it looks at the current stock, Equities, finds current value and the previous value the day before and calculates the % difference

 

Appreciate I could approach this in DAX but considering M in the first instance

 

TIA

 

DateAttributeValuePercent
31/12/2020Equities2693.637 
31/12/2020Government Bonds251.95 
31/12/2020Credit303.92 
31/12/2020Alternatives16543.53 
01/01/2021Equities2693.6370.00%
01/01/2021Government Bonds251.950.00%
01/01/2021Credit303.92050.00%
01/01/2021Alternatives16543.530.00%
02/01/2021Equities2693.6370.00%
02/01/2021Government Bonds251.950.00%
02/01/2021Credit303.92050.00%
02/01/2021Alternatives16543.530.00%
03/01/2021Equities2693.6370.00%
03/01/2021Government Bonds251.950.00%
03/01/2021Credit303.92050.00%
03/01/2021Alternatives16543.530.00%
04/01/2021Equities2673.853-0.73%
04/01/2021Government Bonds252.380.17%
04/01/2021Credit303.97290.02%
04/01/2021Alternatives16543.530.00%
05/01/2021Equities2688.2230.54%
05/01/2021Government Bonds252.440.02%
05/01/2021Credit303.30260.22%
05/01/2021Alternatives16543.530.00%
06/01/2021Equities2703.6010.57%
06/01/2021Government Bonds251.42-0.40%
06/01/2021Credit301.948-0.45%
06/01/2021Alternatives16543.530.00%
07/01/2021Equities2738.7711.30%
07/01/2021Government Bonds250.58-0.33%
07/01/2021Credit301.5959-0.12%
07/01/2021Alternatives16543.530.00%
08/01/2021Equities2758.1440.71%
08/01/2021Government Bonds250.620.02%
08/01/2021Credit301.5572-0.01%
08/01/2021Alternatives16543.530.00%
1 ACCEPTED SOLUTION
danakajoel
Frequent Visitor

thank you for the replies and help. I have this now working in DAX but will unpick the Power Query method as I believe that would ultimately be more elegant. Thanks again

View solution in original post

3 REPLIES 3
danakajoel
Frequent Visitor

thank you for the replies and help. I have this now working in DAX but will unpick the Power Query method as I believe that would ultimately be more elegant. Thanks again

v-yingjl
Community Support
Community Support

Hi @danakajoel ,

Power Query method:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdHBagMhEIDhVyl7DrM64+h4bEPoQ4QcCtnDQruhic3zVwILdcxW8SL4M34wx+OABs1ocSQ77IbD98+c5umWr+gjgacwnHYqer/cp+vyNS3p5e2ynB8xW4hcp/vrdJ5TvpAhiFgHr58pz/pI8/3xqfXsCJjW0I75/O9ak6ZqDZXJVEGHCdsm7DVhy4R9JmqbqNdELRP1mZw2BQKpk6cmBBIdlqaAUQcdJtYmEUCskg2TczosTGTQ66DD5JUp5FHeWJ1s7M6hDv+Y8mqd6PcOUtAkEgjB6uQpyQCLDgsSR4466DCJNrGALTci2yaPOixNHKpg03T6BQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Attribute = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Attribute", type text}, {"Value", type number}}),
    #"Grouped Rows" = 
        Table.Group(
            #"Changed Type", {"Attribute"}, 
                {
                    {
                        "Data", each 
                        let tab = Table.AddIndexColumn(_,"Index",1,1,Int64.Type) in 
                            Table.AddColumn(
                                tab, "New",
                                (x)=> try Table.Max(Table.SelectRows(tab,(y)=>y[Index]=x[Index]-1),"Index")[Value]
                                    otherwise null 
                            )
                    }
                }
        ),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"Date", "Attribute", "Value", "New"}, {"Data.Date", "Data.Attribute", "Data.Value", "Data.New"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Data", "Percent", each if [Data.New] = null then null else 
[Data.Value] / [Data.New] - 1),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute", "Data.New"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Data.Date", "Date"}, {"Data.Attribute", "Attribute"}, {"Data.Value", "Value"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}, {"Attribute", type text}, {"Value", type number}, {"Percent", Percentage.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Date", Order.Ascending}})
in
    #"Sorted Rows"

Dax method:

Dax percent = 
VAR _d = [Date]
VAR _a = [Attribute]
VAR pre =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Attribute] = _a
                && 'Table'[Date]
                    = CALCULATE (
                        MAX ( 'Table'[Date] ),
                        FILTER ( ALL ( 'Table' ), 'Table'[Attribute] = _a && 'Table'[Date] < _d )
                    )
        )
    )
RETURN
    IF ( ISBLANK ( pre ), BLANK (), [Value] / pre - 1 )

vyingjl_0-1632122508445.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Jakinta
Solution Sage
Solution Sage

This can help...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdHBasMwDIDhVxk5F8WWLFs+bmPsIUoPg+YQ2FLWZn3+mcBgrmzH4IMOP/IHOh4HsqPFEQ2a4TC8ff/M6zzd0og+EngKw+nwEL1f7tN1+ZqW9enlspy3mC1E1unrdTrPaxrIEETUwfPnmnZ9rPN9+9R6dgRMW2jsmF4KbUOWRW1Zlj7ITCFp2rDHhv023Ldhr416bNRvo30b9dpczRYIpBQVbQgkOs1tAaNOmjau2UQAsRBVbM7pNLORQa+Tps1XbCGt88bqqHJThzr9Z0tHd6KLJi3UaCQQgtVRkWaARacZjSNHnTRtUrOxgP27k+zbPOo0t3EoJFXb6Rc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Attribute = _t, Value = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Value", type number}}),
    DateLocale = Table.TransformColumnTypes(ChangedType, {{"Date", type date}}, "hr-HR"),
    Custom1 = Table.AddIndexColumn(DateLocale, "Index",0,1),
    Grouped = Table.Group(Custom1, {"Attribute"}, {{"Gr", each let t=Table.AddIndexColumn( Table.Sort( _ , {"Date", Order.Ascending}), "Index2",-1,1) in Table.AddColumn( t, "Percent", each try ([Value] - t[Value]{[Index2]})/[Value] otherwise 0 ) , type table }}),
    Removed = Table.RemoveColumns(Grouped,{"Attribute"}),
    Expanded = Table.ExpandTableColumn(Removed, "Gr", {"Date", "Attribute", "Value", "Index", "Index2", "Percent"}, {"Date", "Attribute", "Value", "Index", "Index2", "Percent"}),
    #"Changed Type" = Table.TransformColumnTypes(Expanded,{{"Percent", Percentage.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index", "Index2"})
in
    #"Removed Columns"

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Kudoed Authors