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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
jeanramonyap
Helper I
Helper I

Refer to row for a new column

I have a table with the first 4 columns and I would like to create the last column. What is the best way to go about this? 

 

I have seen people use rank but will this work when I have more and more rows of data? What can I do when I will have a year 2025-01 in my dataset?

 

IDYearMonthQuantity 1Prior Month Quantity 1
12342024041000
1234202405250100
1234202406300250
5678202404-1500
5678202405300-150
5678202406200300
22222024042000
2222202405-150200
2222202406100-150

 

 

1 ACCEPTED SOLUTION
BenjaminSNN
Frequent Visitor

Hey!

I think this is best done by adding a custom column in Power Query.
Basically you create a unique ID based on ID-Year-Month and a ID for previous month ID-Year-(Month -1) then you can join the query with itself to get values from the previous month within that ID.

Code:

let
Source =YOURSOURCE,
 #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"Quantity 1", Int64.Type}}),
#"+ TempId" = Table.AddColumn(#"Changed Type", "TempID", each Text.From([ID]) & "-" & Text.From([Year]) & "-" & Text.From([Month])),
#"+ TempId_prev" = Table.AddColumn(#"+ TempId", "TempId_prev", each Text.From([ID]) & "-" & Text.From([Year]) & "-" & Text.From([Month] - 1)),
#"Self-join" = Table.NestedJoin(#"+ TempId_prev", {"TempId_prev"}, #"+ TempId_prev", {"TempID"}, "Added Custom", JoinKind.LeftOuter),
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Self-join", "Added Custom", {"Quantity 1"}, {"Prior Month Quantity 1"}),
#"Replaced Value" = Table.ReplaceValue(#"Expanded Added Custom",null,0,Replacer.ReplaceValue,{"Prior Month Quantity 1"}),
#"Sorted Rows" = Table.Sort(#"Replaced Value",{{"TempID", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"TempID", "TempId_prev"})
in
#"Removed Columns"

BenjaminSNN_0-1720622885837.png

 



View solution in original post

3 REPLIES 3
v-tangjie-msft
Community Support
Community Support

Hi @jeanramonyap ,

 

Thanks @BenjaminSNN  for the quick reply and solution. Please allow me to offer other idea:

We can create a column in PBI Desktop.

Prior Month Quantity 1 = 
var _date=DATE([Year],[Month],1)
var _last_date=EOMONTH(_date,-1)
var _qua= CALCULATE(SUM('Table'[Quantity 1]),FILTER('Table',[ID]=EARLIER('Table'[ID]) && [Year]=YEAR(_last_date)&& [Month]=MONTH(_last_date)))
RETURN IF(ISBLANK(_qua),0,_qua)

Then the result is as follows.

vtangjiemsft_0-1720686049253.png

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

BenjaminSNN
Frequent Visitor

Hey!

I think this is best done by adding a custom column in Power Query.
Basically you create a unique ID based on ID-Year-Month and a ID for previous month ID-Year-(Month -1) then you can join the query with itself to get values from the previous month within that ID.

Code:

let
Source =YOURSOURCE,
 #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"Quantity 1", Int64.Type}}),
#"+ TempId" = Table.AddColumn(#"Changed Type", "TempID", each Text.From([ID]) & "-" & Text.From([Year]) & "-" & Text.From([Month])),
#"+ TempId_prev" = Table.AddColumn(#"+ TempId", "TempId_prev", each Text.From([ID]) & "-" & Text.From([Year]) & "-" & Text.From([Month] - 1)),
#"Self-join" = Table.NestedJoin(#"+ TempId_prev", {"TempId_prev"}, #"+ TempId_prev", {"TempID"}, "Added Custom", JoinKind.LeftOuter),
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Self-join", "Added Custom", {"Quantity 1"}, {"Prior Month Quantity 1"}),
#"Replaced Value" = Table.ReplaceValue(#"Expanded Added Custom",null,0,Replacer.ReplaceValue,{"Prior Month Quantity 1"}),
#"Sorted Rows" = Table.Sort(#"Replaced Value",{{"TempID", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"TempID", "TempId_prev"})
in
#"Removed Columns"

BenjaminSNN_0-1720622885837.png

 



yes, agree that generally speaking power query is better, because it can scale up to larger datasets. DAX option may run into performance issues.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors
Top Kudoed Authors