cancel
Showing results 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

Helper III

## In DAX, add columns from unrelated table, based on sorting and conditions

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!

4 REPLIES 4
Community Champion

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.

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696
Helper III

In case I had both these tables in Power Query, would it be possible then to do this operation in Power Query?

Community Champion

plz use this DAX formula:

DesiredTable =
VAR BudgetTable = 'Table A'
VAR ResultTable = 'Table B'
VAR ResultBudgetTable =
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
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
)
)

Helper III

When I enter your formula, it seems like it doesn't recognize [budget], when it comes to this VAR:
VAR RemainingBudget = [Budget]

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

#### Power BI Monthly Update - August 2024

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

#### Fabric Community Update - August 2024

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

Top Solution Authors
Top Kudoed Authors