Reputation: 17
I am new in Power BI, have a table in excel. I need to create the same in Power BI.
I have one table in Excel which has the following fields:
After first sorting the table based on ID and Date, and then adding Column A, Column B and Column C, where Column A value is equal to previous value of Column C and Column C value is equal to sum of Column A and Column B. The desired table would be:
where,
Column A = IF($A2=$A1,$G1,0)
Column B = D2-E2
Column C = SUM(E2:F2)
How to achieve this in Power BI?
Upvotes: 1
Views: 4740
Reputation: 5192
I believe this may be something like what you are looking for. It requires an index so that you can use the index to reference the previous row's values.
Instead of calculating Column A from Column C like your formula suggested ($G1), I calculated it from [Revenue]. That's because Column C didn't exist yet, and wouldn't exist yet, because it wouldn't be calculated until Column A (E in your formula) exists.
Anyhow, if your table is named Table1 and has [ID], [Name], [Date], and [Revenue] just like in your first image, then you should be able to use this code. Otherwise, change "Table1" to whatever your table name is. Also, you'll need to replace "YourPathAndFile.xls" below with your path and file name--something like "C:\Users\yourname\somefolder\filename.xls". Include the quotes for both the table name and the path and file, just like below.
let
Source = Excel.Workbook(File.Contents("YourPathAndFile.xls"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"ID", type text}, {"Name", type text}, {"Date", type date}, {"Revenue", Currency.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Ascending}, {"Date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Column A", each if [Index] = 0 then 0 else if [ID] = #"Added Index"{[Index]-1}[ID] then #"Added Index"{[Index]-1}[Revenue] else 0),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom B", each [Revenue] - [Column A]),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom C", each [Column A]+[Custom B]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index"})
in
#"Removed Columns"
What I did was:
The result is:
Upvotes: 1