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
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
Super User
Super User

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()
)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

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()
)

@ 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!:
The Definitive Guide to Power Query (M)

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
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.

Top Solution Authors