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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MarianneElver
Helper III
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

YearBudget
20252000
20261900
20271800
20281700


Table B

IDPriority (sorting value)Result
A9100
B8500
C81500
D7300
E680
F61200


The desired table is this:

YearBudgetResultIDResult accumulated by year (nice to)Priority (nice to)
20252000100A1009
20252000500B6008
202619001500C15008
20261900300D18007
2027180080E806
202718001200F12806


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
AmiraBedh
Resident Rockstar
Resident Rockstar

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
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
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

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?

123abc
Community Champion
Community Champion

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:

Udklip.PNG

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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