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
nada
New Member

how to calculate growth as column with categories (in query)

Hello 

sumx(filter(Table, [city] = earlier([city]) &&[commodity] = earlier([commodity]) && eomonth([date],+1) = eomonth(earlier([date]),0)), Table[Value])

 

how can i transfer the code in (query)

 

i want to unpivot the column ot it doesn't work in dax 

 

Solved: Re: how to calculate growth as column with categor... - Microsoft Power BI Community

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @nada 

 

You can try Group By feature to get monthly sum values and then add a custom column to get previous month sales.

1. Group by the first four columns and get Sum of sales into a new Sales column. 

22011001.jpg

 

2. Add a custom column with below code. You can modify it accordingly. 

List.First(Table.Column(Table.SelectRows(#"Grouped Rows", (x)=> x[city]=[city] and x[year]=[year] and x[date]=Date.AddMonths([date],-1)),"Sales"), null)

22011002.jpg

 

Full code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFTSUfJKzCtNLKoEsVJTUhIzgAxDfUN9qKypqVKsDpFqDQ2MiFdsYYJdbVBmZWIKmlpLMxLUGpOg1hBZrVtqUhG6g42QFFuQoNjQwJgUo01wKIY7GlmxESmKSTIZGMyxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [year = _t, month = _t, city = _t, date = _t, sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"year", Int64.Type}, {"month", type text}, {"city", type text}, {"date", type date}, {"sales", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"year", "month", "city", "date"}, {{"Sales", each List.Sum([sales]), type nullable number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Previous Month Sales", each List.First(Table.Column(Table.SelectRows(#"Grouped Rows", (x)=> x[city]=[city] and x[year]=[year] and x[date]=Date.AddMonths([date],-1)),"Sales"), null))
in
    #"Added Custom"

 

You can paste above code into a new blank query's Advanced editor to see the detailed steps. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @nada 

 

You can try Group By feature to get monthly sum values and then add a custom column to get previous month sales.

1. Group by the first four columns and get Sum of sales into a new Sales column. 

22011001.jpg

 

2. Add a custom column with below code. You can modify it accordingly. 

List.First(Table.Column(Table.SelectRows(#"Grouped Rows", (x)=> x[city]=[city] and x[year]=[year] and x[date]=Date.AddMonths([date],-1)),"Sales"), null)

22011002.jpg

 

Full code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFTSUfJKzCtNLKoEsVJTUhIzgAxDfUN9qKypqVKsDpFqDQ2MiFdsYYJdbVBmZWIKmlpLMxLUGpOg1hBZrVtqUhG6g42QFFuQoNjQwJgUo01wKIY7GlmxESmKSTIZGMyxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [year = _t, month = _t, city = _t, date = _t, sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"year", Int64.Type}, {"month", type text}, {"city", type text}, {"date", type date}, {"sales", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"year", "month", "city", "date"}, {{"Sales", each List.Sum([sales]), type nullable number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Previous Month Sales", each List.First(Table.Column(Table.SelectRows(#"Grouped Rows", (x)=> x[city]=[city] and x[year]=[year] and x[date]=Date.AddMonths([date],-1)),"Sales"), null))
in
    #"Added Custom"

 

You can paste above code into a new blank query's Advanced editor to see the detailed steps. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

jennratten
Super User
Super User

Hello!  Below are links that explain different methods that could be implemented for something like this, however, determining which is more applicable and how to customize it to fit your needs will be based on what your data looks like.  If you will post a sample of your data I can help further. 

https://www.thebiccountant.com/2018/09/30/memory-efficient-clustered-running-total-in-power-bi/ 

https://www.thebiccountant.com/2018/07/12/fast-and-easy-way-to-reference-previous-or-next-rows-in-po... 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

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.

Top Solution Authors