Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
Hi,
I have tried various formulas, forums and AI, but I haven't succeeded yet in finding a solution on this tricky problem.
The goal is either to add columns to an existing table, or create a new table.
I have the following:
Table A
Year | Budget |
2025 | 2000 |
2026 | 1900 |
2027 | 1800 |
2028 | 1700 |
Table B
ID | Priority (sorting value) | Result |
A | 9 | 100 |
B | 8 | 500 |
C | 8 | 1500 |
D | 7 | 300 |
E | 6 | 80 |
F | 6 | 1200 |
The desired table is this:
Year | Budget | Result | ID | Result accumulated by year (nice to) | Priority (nice to) |
2025 | 2000 | 100 | A | 100 | 9 |
2025 | 2000 | 500 | B | 600 | 8 |
2026 | 1900 | 1500 | C | 1500 | 8 |
2026 | 1900 | 300 | D | 1800 | 7 |
2027 | 1800 | 80 | E | 80 | 6 |
2027 | 1800 | 1200 | F | 1280 | 6 |
The goal is to have a table where, to each year in Table A, add ID's and values (Result) from Table B, only until the sum of Results is less or equal to the Budget in Table A.
When the sum of Result from Table B exceeds the Budget in Table A that year, then proceed to the next year before adding new ID's from Table B. Like in the desired table above, where the sum of Result when adding ID C to ID A&B was 2200, which is larger than Budget (2000). So I had to add the ID to 2026 instead of 2025.
When moving to a new year, the sum is reset to zero and we start summing again.
As Table B is sorted by the priority column, so we have to start adding ID's from the top of Table B, respecting the sorting.
The start year 2025, and then we move on from there.
The accumulated value of Result as well as Priority are only nice to have.
Table A and B are not related in anyway, and mainly exists as DAX tables or columns.
Hope this makes sense.
The solution can either be a new table, or just adding columns to table A.
Any help is highly appreciated!
Just to comment on your request, DAX doesn't support procedural programming paradigms like loops, iterations, or explicit row-by-row manipulations, which would be ideal for your problem. DAX is designed for fast set-based operations and not for iterating through tables to calculate running totals that depend on previous rows.
Thanks for your comment.
In case I had both these tables in Power Query, would it be possible then to do this operation in Power Query?
plz use this DAX formula:
DesiredTable =
VAR BudgetTable = 'Table A'
VAR ResultTable = 'Table B'
VAR ResultBudgetTable =
ADDCOLUMNS (
ADDCOLUMNS (
ADDCOLUMNS (
SUMMARIZE ( BudgetTable, [Year], [Budget] ),
"BudgetYear", [Year]
),
"CumulativeBudget", [Budget]
),
"ResultYear", BLANK (),
"ID", BLANK (),
"Result", BLANK (),
"Priority", BLANK ()
)
VAR CurrentYear = MINX ( FILTER ( ResultBudgetTable, [ResultYear] = BLANK () ), [BudgetYear] )
VAR CurrentBudget = MINX ( FILTER ( ResultBudgetTable, [ResultYear] = BLANK () ), [CumulativeBudget] )
VAR RemainingBudget = [Budget]
VAR AccumulatedResult = 0
RETURN
ADDCOLUMNS (
ResultBudgetTable,
"ResultYear",
IF ( [ResultYear] = BLANK () && RemainingBudget >= [Result], CurrentYear, [ResultYear] ),
"ID",
IF (
[ResultYear] = BLANK ()
&& RemainingBudget >= [Result]
&& CurrentYear = [BudgetYear],
FIRSTNONBLANK ( ResultTable[ID], ResultTable[Priority] )
),
"Result",
IF (
[ResultYear] = BLANK ()
&& RemainingBudget >= [Result]
&& CurrentYear = [BudgetYear],
FIRSTNONBLANK ( ResultTable[Result], ResultTable[Priority] )
),
"Priority",
IF (
[ResultYear] = BLANK ()
&& RemainingBudget >= [Result]
&& CurrentYear = [BudgetYear],
FIRSTNONBLANK ( ResultTable[Priority], ResultTable[Priority] )
),
"CumulativeBudget",
IF (
[ResultYear] = BLANK ()
&& RemainingBudget >= [Result]
&& CurrentYear = [BudgetYear],
RemainingBudget - [Result],
[CumulativeBudget]
),
"RemainingBudget",
IF (
[ResultYear] = BLANK ()
&& RemainingBudget >= [Result]
&& CurrentYear = [BudgetYear],
RemainingBudget - [Result],
[CumulativeBudget]
),
"AccumulatedResult",
IF (
[ResultYear] = BLANK ()
&& RemainingBudget >= [Result]
&& CurrentYear = [BudgetYear],
AccumulatedResult + [Result],
AccumulatedResult
)
)
Thanks for the quic reply!
When I enter your formula, it seems like it doesn't recognize [budget], when it comes to this VAR:
VAR RemainingBudget = [Budget]
Please se below:
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
112 | |
95 | |
92 | |
35 | |
29 |