cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper V
Helper V

DAX: Selectedvalue in IF doesn't work

Hi guys,

 

I have the following measure:

 

Werkuren per maand =
IF (
SELECTEDVALUE ( Workers[Name] ) = "Test Persoon";
CALCULATE ( SUM ( 'Calendar'[Werkdagen 4 dagen] ) ) * [Werkuren 9];
CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 8]
)

 

This doesn't work in my table, see image below:

filter workhours.jpg

I've tested it with BLANK() and it looks it doesn't recognize the workername because BLANK() gives me the expected value.

If I would like to add more names to the formula, I get the following error:

 

 

Werkuren per maand =
IF (
SELECTEDVALUE ( Workers[Name] ) = "Test Persoon" || "Test Achternaam";
CALCULATE ( SUM ( 'Calendar'[Werkdagen 4 dagen] ) ) * [Werkuren 9];
CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 8]

 

Cannot convert type Text to type True / False.

filter workhours error.jpg

 


Besides this piece of formula I would like to extend it with more IF situations. Does this work like this?

[i]example[/i]

Werkuren per maand = 
IF (
SELECTEDVALUE ( Workers[Name] ) = "Test Persoon";
CALCULATE ( SUM ( 'Calendar'[Werkdagen 4 dagen] ) ) * [Werkuren 9];

       IF (
       SELECTEDVALUE ( Workers[Name] ) = "Test Achternaam";
       CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 8];

                    CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 9];         
)

I think the 'IF' scenario's will be limited to 3 IF's max so I think this option will be allright?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User II
Super User II

Re: DAX: Selectedvalue in IF doesn't work

Alright then! 

 

Then i have some things that I would like you to check:

 

* Do a new measure, Measure = SELECTEDVALUE(Workers[Name])

* Place this measure next to the name column and compare the values. If they are not the same something is off. 

* Also check the format of the name column. 

 

I tried to recreate your problem but mine seems to work...

 

hours/month = IF(SELECTEDVALUE(Table1[Person])="TestP";
    SUMX(Table1;Table1[9workinghours]*Table1[9hrmonthdays]);
    SUMX(Table1;Table1[8workinghours]*Table1[8hrmonthdays])
    )

image.png


Connect on LinkedIn

View solution in original post

7 REPLIES 7
Highlighted
Super User II
Super User II

Re: DAX: Selectedvalue in IF doesn't work

That result seems to indicate that selectedvalue() is finding more than one result in the name column. Have you filtered in correctly to only have Test Persoon?

 

 / J


Connect on LinkedIn
Highlighted
Helper V
Helper V

Re: DAX: Selectedvalue in IF doesn't work

@tex628

I've tested it with multiple values (for example test person, test person 1, test person 2) but I get the same results. Or do I misunderstand your reply? Do you need more data?

 

As I've posted before

Werkuren per maand =
IF (
SELECTEDVALUE ( Workers[Name] ) = "Test Persoon";
CALCULATE ( SUM ( 'Calendar'[Werkdagen 4 dagen] ) ) * [Werkuren 9];
CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 8]
)

So normally this would work with this test person.

Highlighted
Super User II
Super User II

Re: DAX: Selectedvalue in IF doesn't work

Selectedvalue(Column, alternative) works like this:

 

The function looks into a specific column and if there is only one value in that column it returns that specific value.

 

If there are more than one value in your column Selectedvalue will return the specified alternative or blank() if you did not specify an alternative. 

 

Due to the fact that your first if statement "Selectedvalue(table[column]) = blank()" returns true - this makes me think that there is currently more values that only test persoon in that column. 

 

Place a slicer and filter on test persoon to make sure that there is only one distinct value in that column. 

 

Im sorry that this might be messy, but im quitting work now so im a little stressed with the typing.

 

Br,

J


Connect on LinkedIn
Highlighted
Helper V
Helper V

Re: DAX: Selectedvalue in IF doesn't work

Hi @tex628 ,

 

Thanks for your reply. Hope you had a good weekend.

I know what you mean but it doesn't seem to work. Look at my example below, where my DAX is:

 

Case 1:

Werkuren per maand =
IF (
SELECTEDVALUE ( Workers[Name] ) = "Test Persoon";
CALCULATE ( SUM ( 'Calendar'[Werkdagen 4 dagen] ) ) * [Werkuren 9];
CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 8]
)

 

Case 2:

Werkuren per maand =
IF (
SELECTEDVALUE ( Workers[Name] ) = "Test Werknemer";
CALCULATE ( SUM ( 'Calendar'[Werkdagen 4 dagen] ) ) * [Werkuren 9];
CALCULATE ( SUM ( 'Calendar'[IsWorkDay 5 dagen] ) ) * [Werkuren 8]
)

 

It seems to effect the totals, but the values for each month don't change. So: the total is right (the 'true' in my statement), the values each month are wrong (the 'false' in my statement). 

I can't find the right way to do this.

filter workhours1.jpg

Highlighted
Super User II
Super User II

Re: DAX: Selectedvalue in IF doesn't work

Alright then! 

 

Then i have some things that I would like you to check:

 

* Do a new measure, Measure = SELECTEDVALUE(Workers[Name])

* Place this measure next to the name column and compare the values. If they are not the same something is off. 

* Also check the format of the name column. 

 

I tried to recreate your problem but mine seems to work...

 

hours/month = IF(SELECTEDVALUE(Table1[Person])="TestP";
    SUMX(Table1;Table1[9workinghours]*Table1[9hrmonthdays]);
    SUMX(Table1;Table1[8workinghours]*Table1[8hrmonthdays])
    )

image.png


Connect on LinkedIn

View solution in original post

Highlighted
Helper V
Helper V

Re: DAX: Selectedvalue in IF doesn't work

@tex628 , thanks a lot. You made my day! I don't know exactly how but it works and that's fine to me.

Highlighted
Post Prodigy
Post Prodigy

Re: DAX: Selectedvalue in IF doesn't work

Brilliant answer @tex628 It worked for me as well.

Helpful resources

Announcements
Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors