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
tgjones43
Helper IV
Helper IV

DAX calculation depending on values in 3 columns

Hi all

 

I need to create a measure in DAX that multiplies the value in [Count] by a fixed number (not shown here) that is dependent on the values in 3 other columns. [Width] is a numeric column, with the condition being either <5 or >5. [Species] and [Age] are text columns that have either a value of "A" or "B". So there are 8 possible comibinations, as shown below.

 

WidthSpeciesAgeCount
3AA10
7AA10
3AB10
7AB10
3BA10
7BA10
3BB10
7BB10

 

I assume I need to use IF and AND statements but having had several attempts I cannot get it to work. Can anyone help please?

1 ACCEPTED SOLUTION
tex628
Community Champion
Community Champion

Measure =
VAR Width = IF(Selectedvalue([Width])<5;"A";"B")
VAR Species = Selectedvalue([Species])
VAR Age = Selectedvalue([Age])
Return
IF(
Width = "A" &&
Species = "A" &&
Age = "A" ; 1 ;
IF(
Width = "A" &&
Species = "A" &&
Age = "B" ; 2 ;
IF(
Width = "A" &&
Species = "B" &&
Age = "B" ; 3 ;
IF(
Width = "B" &&
Species = "B" &&
Age = "B" ; 4 ;
IF(
Width = "B" &&
Species = "B" &&
Age = "A" ; 5 ;
IF(
Width = "B" &&
Species = "A" &&
Age = "A" ; 6 ;
IF(
Width = "B" &&
Species = "A" &&
Age = "B" ; 7 ;
IF(
Width = "A" &&
Species = "B" &&
Age = "A" ; 8 ; BLANK()
))))))))

Replace the numeric values in red with your fixed values


Connect on LinkedIn

View solution in original post

9 REPLIES 9
jthomson
Solution Sage
Solution Sage

One way would be to make a calculated column that's something like:

 

ConstantLookupValue = 

var widthcheck = if ([width] <5,0,1)

var agecheck = if([Age]="A",0,2)

var speciescheck = if([Species]="A",0,4)

return widthcheck+agecheck+speciescheck

 

This'll return a value between 0 and 7, that relates to each row in the table you have - then you can relate it to a static table with numbers 0-7 and the related constant you haven't shown

Hi @jthomson That looks good, but I need a modification for my data. The issue is that the [Width] column is from one table and [Age] and [Species] are from a different table and the tables are connected by a One to Many relationship, i.e. for each value of [Width] there are several values of [Age] and [Species].

 

Is it still possible to do the calculation in this scenario?

 

Thank you.

hi, @tgjones43 

It could be done, Please share a simple sample pbix file and the expected output.

 

Best Regards,
Lin

 

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-lili6-msft 

 

Thank you, please find a simple sample file here (https://www.dropbox.com/s/zan7g3o1p3w6gvq/Fish_calculation_example.pbix?dl=0) and below is the required output. The final 2 columns are what I require. I would like to add the Fixed Factor as a Measure, and it is a number that depends on [Survey width] (>5 or <5), [Observable] (Atlantic salmon or Brown / sea trout) and [FW age] (>0+ or 0+) - there are 8 possible combinations. The score is then a simple equation from Count and Fixed Factor.

 

In the pbix file I have created the table below in the Report view (minus the final 2 columns). The columns of the visualisation are taken from 3 related tables which are connected by 'One to One' and 'One to Many' relationships.

 

Hopefully this is enough information, but please let me know if you need any more details.

 

Many thanks!

 

Site nameSampling Date/TimeSurvey width (m)ObservableFW ageCountFixed FactorScore
LON0322/07/2004 12:004.15Atlantic salmon>0+10.66432
LON0322/07/2004 12:004.15Brown / sea trout>0+220.675433
LON0322/07/2004 12:004.15Brown / sea trout0+130.593222
LON0310/08/2015 12:005.2Atlantic salmon0+10.51062
LON0310/08/2015 12:005.2Brown / sea trout>0+130.627221
LON0310/08/2015 12:005.2Brown / sea trout0+310.566655

hi, @tgjones43 

What is the logic to calculate Fixed Factor and Score?

If you could add a column in 3 - Observations table by this formula

Measure 2 = var widthcheck = if (SELECTEDVALUE('4 - Observation Attributes'[Survey width (m)]) <5,0,1)

var agecheck = if(SELECTEDVALUE('3 - Observations'[FW age])=">0+",0,2)

var speciescheck = if(SELECTEDVALUE('3 - Observations'[Observable])="Atlantic salmon",0,4)

return speciescheck+widthcheck+agecheck

or use this measure as below:

 

Measure 2 = var widthcheck = if (SELECTEDVALUE('4 - Observation Attributes'[Survey width (m)]) <5,0,1)

var agecheck = if(SELECTEDVALUE('3 - Observations'[FW age])=">0+",0,2)

var speciescheck = if(SELECTEDVALUE('3 - Observations'[Observable])="Atlantic salmon",0,4)

return speciescheck+widthcheck+agecheck

Best Regards,
Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-lili6-msft

Thank you for your help. The Fixed Factor is just a number that is used by the Score column and it depends on values in 3 columns, so if [Survey width] is less than 5m, [Observable] is Atlantic salmon and the [FW Age] is >0+ then the Fixed Factor is 0.6643 (as shown in the first column). The [Score] is just ([Count]/[P value]+0.5).

I tried your formula as a Measure and a Column but they don't quite do what I was hoping (I have refreshed the pbix file in the above link). As a Column, the formula gives a value of 6 no matter what is in the three columns it is looking at. As a Measure, the formula gives different values for the different conditions, which is great, but it brings in extra blank rows for values that aren't present in the original data. The output is shown below, the rows highlighted in red only appear when adding the Measure to the table. Do you know why this is?

 

Site nameSampling Date/TimeSurvey area (km2)Survey width (m)ObservableFW ageCountColumnMeasure
LON0322/07/2004 12:00207.54.15Atlantic salmon>0+160
LON0322/07/2004 12:00  Atlantic salmon0+  6
LON0322/07/2004 12:00207.54.15Brown / sea trout>0+2264
LON0322/07/2004 12:00207.54.15Brown / sea trout0+1366
LON0310/08/2015 12:00  Atlantic salmon>0+  6
LON0310/08/2015 12:002355.2Atlantic salmon0+163
LON0310/08/2015 12:002355.2Brown / sea trout>0+1365
LON0310/08/2015 12:002355.2Brown / sea trout0+3167

 

 

 

tex628
Community Champion
Community Champion

Measure =
VAR Width = IF(Selectedvalue([Width])<5;"A";"B")
VAR Species = Selectedvalue([Species])
VAR Age = Selectedvalue([Age])
Return
IF(
Width = "A" &&
Species = "A" &&
Age = "A" ; 1 ;
IF(
Width = "A" &&
Species = "A" &&
Age = "B" ; 2 ;
IF(
Width = "A" &&
Species = "B" &&
Age = "B" ; 3 ;
IF(
Width = "B" &&
Species = "B" &&
Age = "B" ; 4 ;
IF(
Width = "B" &&
Species = "B" &&
Age = "A" ; 5 ;
IF(
Width = "B" &&
Species = "A" &&
Age = "A" ; 6 ;
IF(
Width = "B" &&
Species = "A" &&
Age = "B" ; 7 ;
IF(
Width = "A" &&
Species = "B" &&
Age = "A" ; 8 ; BLANK()
))))))))

Replace the numeric values in red with your fixed values


Connect on LinkedIn

Thanks all for your help.

 

The solution by @tex628 is the one that worked best for me, although I had to make a small alteration to lines 3 and 4 to include the condition, as below:

 

Measure =
VAR Width = IF(SELECTEDVALUE([Width])<5,"A","B")
VAR Species = IF(SELECTEDVALUE([Species])="Atlantic salmon","A","B")
VAR Age = IF(SELECTEDVALUE([Age])="0+","A","B")
Return
IF(
Width = "A" &&
Species = "A" &&
Age = "A" , 0.5395 ,
IF(
Width = "A" &&
Species = "A" &&
Age = "B" , 0.6643 ,
IF(
Width = "B" &&
Species = "A" &&
Age = "A" , 0.5106 ,
IF(
Width = "B" &&
Species = "A" &&
Age = "B" , 0.5547 ,
IF(
Width = "A" &&
Species = "B" &&
Age = "A" , 0.5932,
IF(
Width = "A" &&
Species = "B" &&
Age = "B" , 0.6754 ,
IF(
Width = "B" &&
Species = "B" &&
Age = "A" , 0.5666 ,
IF(
Width = "B" &&
Species = "B" &&
Age = "B" , 0.6272 , BLANK()
))))))))

hi, @tgjones43 

First, you should know that calculated column and calculate table can't be affected by any slicer. 
Notice:
1. Calculation column/table not support dynamic changed based on filter or slicer.
2. Measure can be affected by filter/slicer, so you can use it to get dynamic summary result.
here is reference:
https://community.powerbi.com/t5/Desktop/Different-between-calculated-column-and-measure-Using-SUM/t...
https://www.sqlbi.com/articles/calculated-columns-and-measures-in-dax/

 

Second, for the blank rows, it relates to the row context and filter context in dax calculation.

for [Observable] is in "3 - Observations" table, if calculation will return two results, so this leads to this case.

just set visual level filter of Survey area (km2) or Survey width (m) is not blank.

 

Best Regards,
Lin

 

 

 

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.