Forum Discussion
Adding Custom Column To Obtain Prior Month Balance
- 3 years ago
JRParker my bad. I wanted to sort by date upon grouping but then changed my mind... Before I give up and commit a suicide, lets replace function f with the following
f = (tbl as table) as table => [sorted = Table.Sort(tbl, "Date"), // Sort the table by the "Date" column prior_month = {0} & List.RemoveLastN(sorted[Balance], 1), // Create a list of prior month balances by removing the last balance value and appending a 0 at the beginning out = Table.FromColumns(Table.ToColumns(sorted) & {prior_month}, Table.ColumnNames(sorted) & {"Prior Month"}) // Add the prior month balances as a new column named "Prior Month" ] [out]
Know there are other Entities and Account Numbers in the table. 🙂
hi, JRParker group your data by Entity and Account Number and then do whatever you want to each group of data. If you have 1 balance per month per every Entity & Account then try this
let
Source = your_table,
f = (tbl as table) as table =>
[sorted = Table.Sort(tbl, "Date"),
prior_month = {0} & List.RemoveLastN( tbl[Balance], 1),
out = Table.FromColumns( Table.ToColumns(tbl) & {prior_month}, Table.ColumnNames(tbl) & {"Prior Month"})] [out],
gr = Table.Group(Source, {"Entity", "Account Number"}, {{"all", each f(_)}}),
expand = Table.ExpandTableColumn(gr, "all", {"Date", "Balance", "Prior Month"}, {"Date", "Balance", "Prior Month"})
in
expand- JRParker3 years agoHelper III
Just saw this post AlienSx ... let me experiment and advise... thanks... Jim
- JRParker3 years agoHelper III
Wicked! AlienSx = 1 ChatGPT = 0
One issue that surfaces presumably having to do with the original source of my table. First, below is a summary of the Power Query code with your code embedded and an explanation of each line by ChatGPT. Note that prior to your code commencing at Step 5, we brought in the Trial Balance table, merged a column named Statement, and filtered the Statement column to only "Income Statement" accounts (vs "Balance Sheet" accounts). Interestingly, the final results of this query include both "Income Statement" and "Balance Sheet" accounts where only "Income Statement" accounts should result. Can you advise how to resolve this?
let
// Step 1: Define the data source as the "Trial Balance" table
Source = #"Trial Balance",
// Step 2: Merge the "Trial Balance" table with the "Account Category" table based on the "Account Number" column
#"Merged Queries" = Table.NestedJoin(Source, {"Account Number"}, #"Account Category", {"Account Number"}, "Account Category", JoinKind.LeftOuter),
// Step 3: Expand the "Account Category" column to include the "Statement" column
#"Expanded Account Category" = Table.ExpandTableColumn(#"Merged Queries", "Account Category", {"Statement"}, {"Account Category.Statement"}),
// Step 4: Filter the rows to include only those with a "Statement" value of "Income Statement"
#"Filtered Rows" = Table.SelectRows(#"Expanded Account Category", each ([Account Category.Statement] = "Income Statement")),
// Step 5: Define a function "f" to perform further operations on each grouped table
f = (tbl as table) as table =>
[sorted = Table.Sort(tbl, "Date"),
prior_month = {0} & List.RemoveLastN( tbl[Balance], 1),
out = Table.FromColumns( Table.ToColumns(tbl) & {prior_month}, Table.ColumnNames(tbl) & {"Prior Month"})] [out],
// Step 6: Group the data by specific columns and apply the function "f" to each group
gr = Table.Group(Source, {"Entity", "Account Number", "Description", "Account # - Description" }, {{"all", each f(_)}}),
// Step 7: Expand the "all" column to include the "Date", "Balance", and "Prior Month" columns
expand = Table.ExpandTableColumn(gr, "all", {"Date", "Balance", "Prior Month"}, {"Date", "Balance", "Prior Month"}),
// Step 8: Change the data types of the "Balance" and "Prior Month" columns to Currency
#"Changed Type" = Table.TransformColumnTypes(expand,{{"Balance", Currency.Type}, {"Prior Month", Currency.Type}})
in
// Step 9: Return the final transformed table
#"Changed Type" - JRParker3 years agoHelper III
An issue most likely due to embedding your code into the pre-existing code, or not articulating the data structure. Here is an excerpt of 2 Acct #s:
1) Note the data starts with 3/31/22
2) There may be an issue if the prior month is in a prior year
3 ) Some of the Prior Month results are seemingly correct (highlighted in red) while other months are not
Entity Account Number Description Date Balance Prior Month FVE 4000-00-00 SALES 1 3/31/2022 ($2,330.08) ($5,972.62) FVE 4000-00-00 SALES 1 4/30/2022 ($4,890.36) ($2,330.08) FVE 4000-00-00 SALES 1 5/31/2022 ($5,972.62) ($13,285.10) FVE 4000-00-00 SALES 1 6/30/2022 ($13,285.10) ($14,388.49) FVE 4000-00-00 SALES 1 7/31/2022 ($14,388.49) ($20,713) FVE 4000-00-00 SALES 1 8/31/2022 ($20,713.00) ($32,338.88) FVE 4000-00-00 SALES 1 9/30/2022 ($32,338.88) ($31,558.75) FVE 4000-00-00 SALES 1 10/31/2022 ($31,558.75) ($47,740.27) FVE 4000-00-00 SALES 1 11/30/2022 ($47,740.27) ($57,606.09) FVE 4000-00-00 SALES 1 12/31/2022 ($49,812.30) ($35,934.17) FVE 4000-00-00 SALES 1 1/31/2023 ($2,910.00) $0 FVE 4000-00-00 SALES 1 2/28/2023 ($15,823.60) ($2,910) FVE 4000-00-00 SALES 1 3/31/2023 ($25,206.91) ($15,823.60) FVE 4000-00-00 SALES 1 4/30/2023 ($35,934.17) ($25,206.91) FVE 4000-00-00 SALES 1 5/31/2023 ($57,606.09) ($49,812.30) WESS 4000 SALES 3 3/31/2022 ($2,275.00) ($265,195.40) WESS 4000 SALES 3 4/30/2022 ($127,130.40) ($2,275) WESS 4000 SALES 3 5/31/2022 ($265,195.40) ($448,113.75) WESS 4000 SALES 3 6/30/2022 ($448,113.75) ($518,720.40) WESS 4000 SALES 3 7/31/2022 ($518,720.40) ($748,675.40) WESS 4000 SALES 3 8/31/2022 ($748,675.40) ($938,658.77) WESS 4000 SALES 3 9/30/2022 ($938,658.77) ($1,090,123.77) WESS 4000 SALES 3 10/31/2022 ($1,090,123.77) ($1,253,208.77) WESS 4000 SALES 3 11/30/2022 ($1,253,208.77) ($1,476,318.77) WESS 4000 SALES 3 12/31/2022 ($1,476,318.77) ($783,390) WESS 4000 SALES 3 1/31/2023 ($183,245.00) $0 WESS 4000 SALES 3 2/28/2023 ($283,535.00) ($183,245) WESS 4000 SALES 3 3/31/2023 ($360,890.00) ($283,535) WESS 4000 SALES 3 4/30/2023 ($554,170.00) ($360,890) WESS 4000 SALES 3 5/31/2023 ($783,390.00) ($554,170) - JRParker3 years agoHelper III
Sorry, my bad Alien Sx ... your solution is working when filtering the table in Power Query... I'm doing something wrong in the visualizations ... will advise
- JRParker3 years agoHelper III
Ok, there is an issue that likely has something to do with the sorting or timing thereof in Power Query. Here is a snapshot of the table after the query is run; note the default date sequence and the Prior Month values relative to the prior row (?) and it gets out of whack at the change of the year. Then note what happens after sorting on the Date column:
Entity Account Number Date Balance Prior Month FVE 4000-00-00 1/31/2023 $ (2,910.00) $ - FVE 4000-00-00 2/28/2023 $ (15,823.60) $ (2,910.00) FVE 4000-00-00 3/31/2023 $ (25,206.91) $ (15,823.60) FVE 4000-00-00 4/30/2023 $ (35,934.17) $ (25,206.91) FVE 4000-00-00 12/31/2022 $ (49,812.30) $ (35,934.17) FVE 4000-00-00 5/31/2023 $ (57,606.09) $ (49,812.30) FVE 4000-00-00 11/30/2022 $ (47,740.27) $ (57,606.09) FVE 4000-00-00 10/31/2022 $ (31,558.75) $ (47,740.27) FVE 4000-00-00 9/30/2022 $ (32,338.88) $ (31,558.75) FVE 4000-00-00 8/31/2022 $ (20,713.00) $ (32,338.88) FVE 4000-00-00 7/31/2022 $ (14,388.49) $ (20,713.00) FVE 4000-00-00 6/30/2022 $ (13,285.10) $ (14,388.49) FVE 4000-00-00 5/31/2022 $ (5,972.62) $ (13,285.10) FVE 4000-00-00 3/31/2022 $ (2,330.08) $ (5,972.62) FVE 4000-00-00 4/30/2022 $ (4,890.36) $ (2,330.08) Entity Account Number Date Balance Prior Month FVE 4000-00-00 3/31/2022 $ (2,330.08) $ (5,972.62) FVE 4000-00-00 4/30/2022 $ (4,890.36) $ (2,330.08) FVE 4000-00-00 5/31/2022 $ (5,972.62) $ (13,285.10) FVE 4000-00-00 6/30/2022 $ (13,285.10) $ (14,388.49) FVE 4000-00-00 7/31/2022 $ (14,388.49) $ (20,713.00) FVE 4000-00-00 8/31/2022 $ (20,713.00) $ (32,338.88) FVE 4000-00-00 9/30/2022 $ (32,338.88) $ (31,558.75) FVE 4000-00-00 10/31/2022 $ (31,558.75) $ (47,740.27) FVE 4000-00-00 11/30/2022 $ (47,740.27) $ (57,606.09) FVE 4000-00-00 12/31/2022 $ (49,812.30) $ (35,934.17) FVE 4000-00-00 1/31/2023 $ (2,910.00) $ - FVE 4000-00-00 2/28/2023 $ (15,823.60) $ (2,910.00) FVE 4000-00-00 3/31/2023 $ (25,206.91) $ (15,823.60) FVE 4000-00-00 4/30/2023 $ (35,934.17) $ (25,206.91) FVE 4000-00-00 5/31/2023 $ (57,606.09) $ (49,812.30)