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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
NilR
Post Patron
Post Patron

Measure in If statement returns incorrect total

I am new in PBI and desperately need help. I have a summarized table and the end result is the last column ( had_well) in the picture, but returns the wrong total which is the first column total. I understand that I need to iterate through ID ( first column in pic) to get right result, but I don't know how.

 

 

 

VAR __tmpTable = 
ADDCOLUMNS(
        SUMMARIZE(
            'AW',
            'AW'[ID],
            "MAXID", CALCULATE(DISTINCTCOUNT('AW'[ID]), ALLEXCEPT('AW','AW'[ID]),'AW'[DATE]=_maxdate)
        ),"COUNTYR1", [YR1_HAD_WELL],"COUNTYR2", [YR2_HAD_WELL])

Return
IF(HASONEVALUE('AW'[M18]),SUMX(FILTER(__tmpTable,[MAXID]=1),[COUNTYR2]),SUMX(FILTER(__tmpTable,[MAXID]=1),[COUNTYR1]))

 

 

 

I also tried this but returned blank total:

 

 

Return
 IF(HASONEFILTER([ID]),SUMX(FILTER(__tmpTable,[MAXID]=1),(IF(_m18="YES",[COUNTYR2],[COUNTYR1]))))

 

 

 

The if statement returns the first column total.

 

 

Thank you!

1 ACCEPTED SOLUTION
NilR
Post Patron
Post Patron

I changed the calculation and worked.

 

ADDCOLUMNS(
        SUMMARIZE(
            ''AW'',
            ''AW''[ID],''AW''[M18],
            "MAXID", CALCULATE(DISTINCTCOUNT(''AW''[ID]), ALLEXCEPT(''AW'',''AW''[ID]),''AW''[DATE]=_maxdate)
        ),"YR1", [YR1_HAD_WELL],"YR2", [YR2_HAD_WELL],"TTL_MO", [TTL_MO],"HAD_WELL",IF([M18]="YES" ,[YR2],[YR1]))
Return
SUMX(FILTER(__tmpTable,[MAXID]=1),[HAD_WELL])

 

View solution in original post

6 REPLIES 6
parry2k
Super User
Super User

@NilR That's' pretty good news what can be better than this. Thanks for letting me know and posting your solution.

 

Cheers!!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

NilR
Post Patron
Post Patron

I changed the calculation and worked.

 

ADDCOLUMNS(
        SUMMARIZE(
            ''AW'',
            ''AW''[ID],''AW''[M18],
            "MAXID", CALCULATE(DISTINCTCOUNT(''AW''[ID]), ALLEXCEPT(''AW'',''AW''[ID]),''AW''[DATE]=_maxdate)
        ),"YR1", [YR1_HAD_WELL],"YR2", [YR2_HAD_WELL],"TTL_MO", [TTL_MO],"HAD_WELL",IF([M18]="YES" ,[YR2],[YR1]))
Return
SUMX(FILTER(__tmpTable,[MAXID]=1),[HAD_WELL])

 

parry2k
Super User
Super User

@NilR throw some sample data and make sure respective tables are in the data model with the relationship.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@NilR oh man there is a lot going on, and very hard to decode it. better you put a sample pbix file and explain what you are looking for and will able to help. I cannot spend time figuring this measure out. sorry about that.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I have big table but I will see what I can do. In the meantime I changed my code and made it smaller. hopefully make more sense now. 🙂

parry2k
Super User
Super User

@NilR try something like this

 

Measure = 
SUMX ( vALUES ( Table[Id] ), 
IF(SELECTEDVALUE('0410 Adult Wellness'[MALE_18-34]) = "YES" , __tmpYR2, __tmpYR1)
)

 

Check my latest blog post Year-2020, Pandemic, Power BI and Beyond to get a summary of my favourite Power BI feature releases in 2020

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.

Top Solution Authors