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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Anonymous
Not applicable

Create a calculated column with condition

Good afternoon

 

I want to create a calculated column as a "Flag" with just 0's and 1's if the units where less than projected. When doing so, I get the addition of all rows where the condition was met, not just using the overall totals, how can I do that?

 

This is my database:

 

STATEIDTYPEUNITSPROJECTED
FLORIDA1234CAR10001050
FLORIDA1234VAN900910
FLORIDA1234SUV800840
FLORIDA4573BIKE760800
FLORIDA4573CAR890900
FLORIDA4578VAN755700

 

This is my formula for the "flag" column:

FLAG = IF (UNITS < PROJECTED, 1, 0)

 

And this is what I am getting:

 

STATEIDUNITSPROJECTEDCURRENT FLAGEXPECTED FLAG
FLORIDA12342700280031
FLORIDA45731650170021
FLORIDA457875570000

 

I understand why the current flag returns those numbers (it is adding the total times the condition was met by "type" and "ID", but I need that flag to be the EXPECTED FLAG column, with just 0's and 1's.

 

Please note that the TYPE column will be a slicer in the dashboard, not included in the table, so I need the final table to show if the overall ID units (depending on option(s) selected) where less than projections.

 

Thank you for your support.

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Anonymous  try this as a calculated column

 

flag =
VAR _units =
    CALCULATE ( SUM ( t2[UNITS] ), ALLEXCEPT ( t2, t2[ID] ) )
VAR _proj =
    CALCULATE ( SUM ( t2[PROJECTED] ), ALLEXCEPT ( t2, t2[ID] ) )
RETURN
    IF ( _units < _proj, 1, 0 )

 

smpa01_0-1634240642036.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

14 REPLIES 14
Tweeg
Helper I
Helper I

Your formula seems fine, it seems like it is summarizing the result. Did you check if its set to "Dont Summarize" ?

See my attached screenshots. (In my screenshot its set to Sum, make sure it isnt)

333.png22.png11.png

smpa01
Super User
Super User

@Anonymous  try this as a calculated column

 

flag =
VAR _units =
    CALCULATE ( SUM ( t2[UNITS] ), ALLEXCEPT ( t2, t2[ID] ) )
VAR _proj =
    CALCULATE ( SUM ( t2[PROJECTED] ), ALLEXCEPT ( t2, t2[ID] ) )
RETURN
    IF ( _units < _proj, 1, 0 )

 

smpa01_0-1634240642036.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

This works perfect as a solution, but when I build a measure I get the wrong result. I want to create a measure that sums (or distinctcounts whichever works) all the 1's obtained in that column.

 

Again, I need to sum just the ID's that had a 1, but I also need the measure to NOT CONSIDER (exclude) or SELECTALL STATES regardless of the slicer built in the dashboard.

 

The idea is to build a measure with the OVERALL result (this case) ignoring what state is selected, just sum/count all 1's at a "system level", and a measure that responds to the state filter (which I already built).

 

Is this possible?

@Anonymous  do you mean this?

 

smpa01_0-1634245494707.png

If not, paste a screenshot of your desired result

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Good morning, 

 

Sorry about my last email, I made a mistake.

 

You are getting the right result, however, the "measure 2" result output (total sum if you would, that you would see in a card visualization) should be two (2) because there were 2 ID's out of the total 3 that had units < projected.

 

You are geeting a total of 5 because it is summarizing all the ONE's I just need a measure that "distinctcounts" or similar the drive ID's that had a one.

 

Also, I need that distinct count NOT TO CONSIDER a state filter that will be in the dashboard, as reference, there is a helper table called "State" that has the unique states.

 

so for example, it would be something like:

 

= caluclate( Distinctcount( ID), ALL(STATE TABLE), MEASURE2=1) but this formula doesn't give me the result I need.

 

You could use my last post to see the updated "test" file you sent me.

 

Thank you for your support. 

@Anonymous  I am loosing you here. Can you please explicitly show the result you want (with numbers)? 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

I am sorry.

 

I want a 5, see the picture, and I also want that measure, (or the new one) to be unresponsive to a STATE slicer that will be in the dashboard (this slicer comes from another table):

 

GIODGR_0-1634307870781.png

 

@Anonymous  and for exact what reason you excluded 7789 from that? Should it not be 6 and not 5?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Yes you are correct, I didn't scroll down enough, it should be 6. It was my mistake.

@ the best I can come up with this

 

Measure 3 = CALCULATE(DISTINCTCOUNT(t2[ID]),FILTER(t2,t2[flag]=1))
forCard = CALCULATE(CALCULATE(DISTINCTCOUNT(t2[ID]),FILTER(t2,t2[flag]=1)),ALL(t2))

 

smpa01_0-1634310049808.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

I thought about that, but the problem is that the card is not responsive to other filters in the dashboard, I have vehicle type, fiscal year, fiscal month, and others.

Anonymous
Not applicable

Hi,

 

Thak you for your answer, but no, that's not what I mean, I have edited your test file so you can understand what's happening to me.

 

If you change the selections, the measure number changes, I need it to always show the total number of 1's (select all options in slicer), regardless of the state I select, that's how I get to show the "system" overall result.

 

See following link (forum doesn't allow me to attach a PBI file)

 

https://1drv.ms/u/s!Av0WzGMqPtTonimUcDlilsZ2DXQz

 

Thank you again so much for your support.

If you want a measure that ignores filters you can try this. Lets say you calculate the sum (easy example)

= SUM(Sales[Flag])

But you want the calculation to work no matter what you filter you can use:

= CALCULATE(SUM(Sales[Flag]), ALL(Sales[STATE]))

 

Anonymous
Not applicable

Right, but if I do that, it doesn't consider just the 1's that I assigned to each ID, it will use 1's on each row that the condition is satisfied.

 

I tried:

 

Measure = CALCULATE(SUM('Sales'[Flag]), ALL('Sales'[STATE]),FILTER('Sales','Sales'[FLAG]=1))

 

But I get a greater value than the one I should get, although now the state slicer doesn't affect my measure.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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