Forum Discussion

Sujit_Thakur's avatar
Sujit_Thakur
Solution Sage
6 years ago
Solved

DAX

Dear PBI Family ,

It's just 2 months that I have started using power bi I as my daily work now

I am stuck in an issue please consider if you can answer to this question so that it helps me alot

I have a measure which calculates from different fields and gives me a value which can be filtered using date slicer.

Let's call this measure as test

Now my problem is I have to put values which I get from this test measure range wise into a table like for example the expected result is as below

 

Name       <0.25        0.25-0.50      0.50-0.75     >0.75

A                  1                   2                   66            23 

B                 12                 22                   6            28 

C                 12                 22                   6            28 

 

Currently I have achieved it using a long way that is I am make four measures and every measures count rows by filtering the table according to the value for example


For <0.25 = Countrows(Filter(test<0.25))

 

I read somewhere regarding this evaluate dax
Which returns a table so I was thinking if is it possible to get my expected result using evaluate

 

 

 

Note - name colunmn in above expected result is a field from a table students  where values are A,B and C
Please project some light on my question thank you

nickyvv  

 

Regards ,

Sujit 

  • v-zhenbw-msft's avatar
    v-zhenbw-msft
    6 years ago

    Hi Sujit_Thakur ,

     

    Do you want to count the number of Model or the number of Name?

    We can create two measures and use the following ways to meet your requirement.

     

    1. Create a Model column in data table.

     

    Model = CALCULATE(MAX('index table'[Model]),FILTER('index table','index table'[Name]='data table'[Name]))

     

     

    2. Create a new table.

     

    Table = CROSSJOIN(VALUES('index table'[Model]),{"<0.25","0.25-0.5","0.5-0.75",">0.75","Total"})

     

     

    3. If you want to count the number of Name, you can use the following measure.

     

    Measure = 
    var _025 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]<0.25 && 'data table'[Model]=MAX('Table'[Model])))
    var _025_05 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]>=0.25&&[Test]<0.5 && 'data table'[Model]=MAX('Table'[Model])))
    var _05_075 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]>=0.5&&[Test]<0.75 && 'data table'[Model]=MAX('Table'[Model])))
    var _075 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]>=0.75&& 'data table'[Model]=MAX('Table'[Model])))
    return
    SWITCH(
        TRUE(),
        MAX('Table'[Value]) = "<0.25",_025,
        MAX('Table'[Value]) = "0.25-0.5",_025_05,
        MAX('Table'[Value]) = "0.5-0.75",_05_075,
        MAX('Table'[Value]) = ">0.75",_075,
        MAX('Table'[Value]) = "Total",_025+_025_05+_05_075+_075)

     

     

    4. If you want to count the number of Model, you can use the following measure.

     

    Measure 2 = 
    var _025 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]<0.25 && 'data table'[Model]=MAX('Table'[Model])))
    var _025_05 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]>=0.25&&[Test]<0.5 && 'data table'[Model]=MAX('Table'[Model])))
    var _05_075 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]>=0.5&&[Test]<0.75 && 'data table'[Model]=MAX('Table'[Model])))
    var _075 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]>=0.75&& 'data table'[Model]=MAX('Table'[Model])))
    return
    SWITCH(
        TRUE(),
        MAX('Table'[Value]) = "<0.25",_025,
        MAX('Table'[Value]) = "0.25-0.5",_025_05,
        MAX('Table'[Value]) = "0.5-0.75",_05_075,
        MAX('Table'[Value]) = ">0.75",_075,
        MAX('Table'[Value]) = "Total",_025+_025_05+_05_075+_075)

     

     

    The SWITCH function means that when the current column is equal to a certain value, output the corresponding value.

    For example, when Table[Value] = “<0.25”, then output the value conforming to <0.25.

     

    If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

     

    Best regards,

     

    Community Support Team _ zhenbw

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

     

    BTW, pbix as attached.

    DAX.pbix27 KB

24 Replies

  • Hi Sujit_Thakur ,

     

    What you can try is created a calculated column which defines your categories as <0.25 , 0.25-0.50 , 0.50-0.75, >0.75 based on your TEST measure.

    Then try using this column in a matrix visual under COLUMN section along with your Name column under ROW section.

     

    Thanks,

    Pragati

     

    • Sujit_Thakur's avatar
      Sujit_Thakur
      Solution Sage

      Dear pragati , I appreciate your response 

       

      But if I created a calculated column my values will not change when I change date selection from my date slicer 

  • v-zhenbw-msft's avatar
    v-zhenbw-msft
    Community Support

    Hi Sujit_Thakur ,

     

    We create a sample. We can use a matrix table and a measure to meet your requirement.

     

    1. Create a new table.

     

    Table 2 = CROSSJOIN(VALUES('Table'[Name]),{"<0.25","0.25-0.5"})

     

     

    2. Then we can create a matrix table based on the new table, create a measure and put it in Values.

     

    Measure 2 = 
    SWITCH(
        TRUE(),
        MAX('Table 2'[Value]) = "<0.25",CALCULATE(COUNT('Table'[Name]),FILTER('Table',[Measure]<0.25 && 'Table'[Name]=MAX('Table 2'[Name]))),
        MAX('Table 2'[Value]) = "0.25-0.5",CALCULATE(COUNT('Table'[Name]),FILTER('Table',[Measure]>=0.25&&[Measure]<0.5 && 'Table'[Name]=MAX('Table 2'[Name]))))

     

     

    If it doesn’t meet your requirement, could you please provide a mockup sample based on fake data or describe the fields of each tables and the relations between tables simply?

    It will be helpful if you can show us the exact expected result based on the tables.

     

    Please upload your files to OneDrive For Business and share the link here. Please don't contain any Confidential Information or Real data in your reply.

     

    Best regards,

     

    Community Support Team _ zhenbw

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

     

    BTW, pbix as attached.

    DAX.pbix20 KB
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Sujit_Thakur 

     

    Add this table to your model. I have named this HP-OP-Bounds.

     

    After that create two calculated columns(HP/OP and Bin) as shown below in your table. I have named this table 'HP-OP'.

     

     

    The formulaes for calculated columns are shown below.

    HP/OP = DIVIDE('HP-OP'[HP], 'HP-OP'[OP], 0)
    
    
    Bin = 
    VAR X = 'HP-OP'[HP/OP]
    VAR Filtered =
        FILTER (
            'HP-OP-Bounds',
            VAR Y = 'HP-OP-Bounds'[Upper Bound] RETURN 'HP-OP-Bounds'[Lower Bound] <= X
                && IF ( ISBLANK ( y ), TRUE (), Y > X )
        )
    RETURN
        MAXX ( Filtered, 'HP-OP-Bounds'[Bin] )

     

    Now use matrix visual as shown below.

     

    Hope this helps.

     

    Appreciate with kudos.

    Please mark it as solution if this resolves your problem.

     

    Thanks

     

     

    • Sujit_Thakur's avatar
      Sujit_Thakur
      Solution Sage

      Dear friend Anonymous ,

      Can I create this same using measure , as I want to later use date slicer , using calculated columns will not help me when I want HP/OP to be recalculated every time I change date range .

       

      You seriously brought me much closer to ans , please answer how can I achieve by using HP/OP as a calculated measure , because as you can see I have date column too of which I will add date slicer .


  • Sujit_Thakur wrote:

    Which returns a table so I was thinking if is it possible to get my expected result using evaluate


    No, EVALUATE is not avaialbe within PBI, at least not in this sense. The only place it could be used is to import data from SSAS, for example.

    • Sujit_Thakur's avatar
      Sujit_Thakur
      Solution Sage

      Dear Arklur  , 

      Is there any way of getting an output as an table using DAX ?

      I have dax studio installed on my system , I read somewhere that I can use evaluate using DAX studio

      • Pragati11's avatar
        Pragati11
        Super User

        Hi Sujit_Thakur ,

         

        EVALUATE is not available as a DAX function in POwer BI.

        When you tallk about EVALUATE in DAX Studio tool, it just serves as a starting query variable to write a dax expresion within it for dax studio tool to execute it.

         

        In DAX studio tool, a DAX expression is identified within EVALUATE clause. So, EVALUATE is just a clause used to write your dax expression in this tool. It's just an statement needed to execute a query within this tool.

        You can read in detail here: https://dax.guide/st/evaluate/

         

        Thanks,

        Pragati

         

    • Greg_Deckler's avatar
      Greg_Deckler
      Community Champion

      Sujit_Thakur - Since you mentioned me I'll chime in. Unless I missed it, I don't have a good sense of your source data. I see the expected output. Can you post a sample of what your source data looks like or point me to where it is posted? Thanks.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Sujit_Thakur  If the segments are fixed i.e. (<0.25        0.25-0.50      0.50-0.75     >0.75). 

    You can use the parameter table pattern.

  • Dear v-zhenbw-msft  Greg_Deckler  and all ,

    following is pasted  data sample for question , please note the measure" Test" is nothing but divide(HP,OP).

    I hope after this sample data v-zhenbw-msft  you can help me with your previously discussed technique

     

    DateName HPOP
    7/1/2020A2322
    7/2/2020A2019
    7/3/2020A2029
    7/4/2020A2240
    7/5/2020A21100
    7/1/2020B2250
    7/2/2020B2150
    7/3/2020B23100
    7/4/2020B2223
    7/5/2020B2017
    7/1/2020C2320
    7/2/2020C2117
    7/3/2020C2215
    7/4/2020C2114
    7/5/2020C2312
    • v-zhenbw-msft's avatar
      v-zhenbw-msft
      Community Support

      Hi Sujit_Thakur ,

       

      We can create a measure to meet your requirement.

       

      Before that we need to create a new table.

       

      Table = CROSSJOIN(VALUES('Data'[Name]),{"<0.25","0.25-0.5","0.5-0.75",">0.75"})

       

       

      Then create a measure like this,

       

      Measure = 
      SWITCH(
          TRUE(),
          MAX('Table'[Value]) = "<0.25",CALCULATE(COUNT('Data'[Name]),FILTER('Data',[Test]<0.25 && 'Data'[Name]=MAX('Table'[Name]))),
          MAX('Table'[Value]) = "0.25-0.5",CALCULATE(COUNT('Data'[Name]),FILTER('Data',[Test]>=0.25&&[Test]<0.5 && 'Data'[Name]=MAX('Table'[Name]))),
          MAX('Table'[Value]) = "0.5-0.75",CALCULATE(COUNT('Data'[Name]),FILTER('Data',[Test]>=0.5&&[Test]<0.75 && 'Data'[Name]=MAX('Table'[Name]))),
          MAX('Table'[Value]) = ">0.75",CALCULATE(COUNT('Data'[Name]),FILTER('Data',[Test]>=0.75 && 'Data'[Name]=MAX('Table'[Name]))))

       

       

      If you have any questions, please kindly ask here and we will try to resolve it.

       

      Best regards,

       

      Community Support Team _ zhenbw

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

       

      BTW, pbix as attached.

      DAX.pbix25 KB
  • Anonymous's avatar
    Anonymous
    Not applicable

    Sujit_Thakur How do you intend too use date slicer?

     

    I guess the HP/OP formulae become SUM(HP)/SUM(OP) for a selected date range right?

    • Sujit_Thakur's avatar
      Sujit_Thakur
      Solution Sage

      Yes Anonymous  ,
      its  SUMX(Table,'Table'[HP]) / SUMX(Table,'Table'[OP])

      • Sujit_Thakur's avatar
        Sujit_Thakur
        Solution Sage

        Anonymous  so that everytime i slice date range , HP / OP is recalculated and values in output table changes 

  • Dear  v-zhenbw-msft  ,

    following is my data 

     1)First table is index table

    Id NameModel
    1AAlpha
    2BAlpha
    3CBravo
    4DBravo
    5ECharlie

    2)Second is data table 

    DateName HPOP
    7/1/2020A2322
    7/2/2020A2019
    7/3/2020A2029
    7/4/2020A2240
    7/5/2020A21100
    7/1/2020B2250
    7/2/2020B2150
    7/3/2020B23100
    7/4/2020B2223
    7/5/2020B2017
    7/1/2020C2320
    7/2/2020C2117
    7/3/2020C2215
    7/4/2020C2114
    7/5/2020C2312
    7/1/2020D21100
    7/2/2020D2250
    7/3/2020D2150
    7/4/2020D23100
    7/5/2020D2223
    7/1/2020E2017
    7/2/2020E2320
    7/3/2020E2317
    7/4/2020E2350
    7/5/2020E23100

    Now what i want as an output is 

    Model    Total  Count    <0.25   0.25-0.50  0.50-0.75   >0.75 

    Alpha                 2              2                            
    Bravo                 2              1                                             1
    Charlie               1             1

    please  also explain your code of switch function  i didnt understood 

    • v-zhenbw-msft's avatar
      v-zhenbw-msft
      Community Support

      Hi Sujit_Thakur ,

       

      Do you want to count the number of Model or the number of Name?

      We can create two measures and use the following ways to meet your requirement.

       

      1. Create a Model column in data table.

       

      Model = CALCULATE(MAX('index table'[Model]),FILTER('index table','index table'[Name]='data table'[Name]))

       

       

      2. Create a new table.

       

      Table = CROSSJOIN(VALUES('index table'[Model]),{"<0.25","0.25-0.5","0.5-0.75",">0.75","Total"})

       

       

      3. If you want to count the number of Name, you can use the following measure.

       

      Measure = 
      var _025 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]<0.25 && 'data table'[Model]=MAX('Table'[Model])))
      var _025_05 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]>=0.25&&[Test]<0.5 && 'data table'[Model]=MAX('Table'[Model])))
      var _05_075 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]>=0.5&&[Test]<0.75 && 'data table'[Model]=MAX('Table'[Model])))
      var _075 = CALCULATE(COUNT('data table'[Name]),FILTER('data table',[Test]>=0.75&& 'data table'[Model]=MAX('Table'[Model])))
      return
      SWITCH(
          TRUE(),
          MAX('Table'[Value]) = "<0.25",_025,
          MAX('Table'[Value]) = "0.25-0.5",_025_05,
          MAX('Table'[Value]) = "0.5-0.75",_05_075,
          MAX('Table'[Value]) = ">0.75",_075,
          MAX('Table'[Value]) = "Total",_025+_025_05+_05_075+_075)

       

       

      4. If you want to count the number of Model, you can use the following measure.

       

      Measure 2 = 
      var _025 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]<0.25 && 'data table'[Model]=MAX('Table'[Model])))
      var _025_05 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]>=0.25&&[Test]<0.5 && 'data table'[Model]=MAX('Table'[Model])))
      var _05_075 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]>=0.5&&[Test]<0.75 && 'data table'[Model]=MAX('Table'[Model])))
      var _075 = CALCULATE(DISTINCTCOUNT('data table'[Model]),FILTER('data table',[Test]>=0.75&& 'data table'[Model]=MAX('Table'[Model])))
      return
      SWITCH(
          TRUE(),
          MAX('Table'[Value]) = "<0.25",_025,
          MAX('Table'[Value]) = "0.25-0.5",_025_05,
          MAX('Table'[Value]) = "0.5-0.75",_05_075,
          MAX('Table'[Value]) = ">0.75",_075,
          MAX('Table'[Value]) = "Total",_025+_025_05+_05_075+_075)

       

       

      The SWITCH function means that when the current column is equal to a certain value, output the corresponding value.

      For example, when Table[Value] = “<0.25”, then output the value conforming to <0.25.

       

      If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

       

      Best regards,

       

      Community Support Team _ zhenbw

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

       

      BTW, pbix as attached.

      DAX.pbix27 KB
      • Sujit_Thakur's avatar
        Sujit_Thakur
        Solution Sage

        Thanks v-zhenbw-msft  

        You have helped me a lot .

        One last doubt , 

        What if my data grown , does it will hamper the Visualization calculation speed ?

        Like now I have only 5 names , what if tomorrow I get data for 5000 names then the crossjoin table would size almost 25000 rows ? I just wanted to ask will this hamper my models efficiency ? About how much space it will take ?

        Please help