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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Complex conditions in a column

Hi!!

 

I'm trying to add a column in a query, that either returns the value of beginning of the year (Opening Balance), year-to-date value (Changes) or beginning of the year value plus year-to-date value (Ending Balance).

Present query looks like this:

CodeDescriptionDateValue
AAAOpening balance01/01/2019100
AAAOpening balance01/02/2019110
AAAOpening balance01/03/2019120
BBBChanges01/01/201910
BBBChanges01/02/201910
BBBChanges01/03/201910
CCCEnding Balance01/01/2019110
CCCEnding Balance01/02/2019120
CCCEnding Balance01/03/2019130

 

The column I would like to add looks like this:

CodeDescriptionDateValueColumn(YTD)
AAAOpening balance01/01/2019100100
AAAOpening balance01/02/2019110100
AAAOpening balance01/03/2019120100
BBBChanges01/01/20191010
BBBChanges01/02/20191020
BBBChanges01/03/20191030
CCCEnding Balance01/01/2019110110
CCCEnding Balance01/02/2019120120
CCCEnding Balance01/03/2019130130

 

I tried to insert a DAX like this but didn't work:

IF ( Code = "AAA" , CALCULATE ( CALCULATE ( Value , ALL (DATE) ) , DATE = "01/01/2019" ) , 

      IF ( Code = "BBB" , CALCULATE ( TOTALYTD ( Value , Date ) , OR ( CODE <> "AAA" , CODE <> "CCC" ) ) ,

            CALCULATE ( CALCULATE ( Value , ALL (DATE) ) , DATE = "01/01/2019" + CALCULATE ( TOTALYTD ( Value , Date ) , OR ( CODE <> "AAA" , CODE <> "CCC" ) ) )

 

I'd appreciate if anyone could save me...! Thank you.

 

Yasuchiki

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

So like this?

Column (YTD) =
SWITCH([Code],
"AAA", VAR __Min = MINX(FILTER('Table',[Code]="AAA"),[Date])
RETURN LOOKUPVALUE('Table'[Value],'Table'[Code],"AAA",'Table'[Date],__Min),
"BBB", SUMX(FILTER('Table','Table'[Date]<=EARLIER('Table'[Date]) && [Code]="BBB"),[Value]),
"CCC", 'Table'[Value],
BLANK()
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

So like this?

Column (YTD) =
SWITCH([Code],
"AAA", VAR __Min = MINX(FILTER('Table',[Code]="AAA"),[Date])
RETURN LOOKUPVALUE('Table'[Value],'Table'[Code],"AAA",'Table'[Date],__Min),
"BBB", SUMX(FILTER('Table','Table'[Date]<=EARLIER('Table'[Date]) && [Code]="BBB"),[Value]),
"CCC", 'Table'[Value],
BLANK()
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thank you sooooo much! I've tried it and it was exactly what I've wanted.

However, what if we have several years in a query and want to get the beginning balance of the year or sum up only the changes during the year? Just like below?

 
CodeDescriptionDateValueValue(YTD)
AAAOpening balance01/01/2019100100
AAAOpening balance01/02/2019110100
AAAOpening balance01/03/2019120100
BBBChanges01/01/20191010
BBBChanges01/02/20191020
BBBChanges01/03/20191030
CCCEnding Balance01/01/2019110110
CCCEnding Balance01/02/2019120120
CCCEnding Balance01/03/2019130130
AAAOpening balance01/01/2020130130
AAAOpening balance01/02/2020150130
AAAOpening balance01/03/2020170130
BBBChanges01/01/20202020
BBBChanges01/02/20202040
BBBChanges01/03/20202060
CCCEnding Balance01/01/2020150150
CCCEnding Balance01/02/2020170170
CCCEnding Balance01/03/2020190190

 

Anonymous
Not applicable

Quite forcefully, I managed to solve this way:
 
Column (YTD) =
SWITCH([Code];
"AAA"; VAR _BB = STARTOFYEAR(Plan1[Date])
RETURN LOOKUPVALUE('Table'[Value];'Table'[Code];"AAA";'Table'[Date];_BB);
"BBB"; VAR _EB = Table[Date] VAR _BB = STARTOFYEAR(Plan1[Date])
RETURN LOOKUPVALUE(Table[Value];Table[Code];"CCC";Table[Date]; _EB) - LOOKUPVALUE('Table'[Value];'Table'[Code];"AAA";'Table'[Date];_BB);
"CCC"; 'Table'[Value];
BLANK()
)

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.