Forum Discussion

vmsouza30's avatar
vmsouza30
Helper I
9 years ago
Solved

Calculation with the variables

In my table there is a grouping (Group  1,2,3) composed of several clients that are not repeated and the only field that is modified is the field "FACTOR".

 

Since my base client is client "E", and the value for the FACTOR field is not fixed, how to develop the DAX function using variables for the following case:

 

  • How to calculate how many customers within each group have a factor above customer "E"?
  • How do you calculate how many customers within each group have a factor below customer "E"?

Help me!!!

 

Table:

 

 

 

  • vmsouza30

     

    Hi,

     

    Maybe you could try these formulas.

     

     

    Above =
    VAR E2014 =
        CALCULATE (
            MIN ( Table1[Factor2014] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2015 =
        CALCULATE (
            MIN ( Table1[Factor2015] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2016 =
        CALCULATE (
            MIN ( Table1[Factor2016] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    RETURN
        CALCULATE (
            COUNTROWS ( Table1 ),
            FILTER (
                Table1,
                'Table1'[Factor2014] > E2014
                    || 'Table1'[Factor2015] > E2015
                    || 'Table1'[Factor2016] > E2016
            )
        )
    Below =
    VAR E2014 =
        CALCULATE (
            MIN ( Table1[Factor2014] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2015 =
        CALCULATE (
            MIN ( Table1[Factor2015] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2016 =
        CALCULATE (
            MIN ( Table1[Factor2016] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    RETURN
        CALCULATE (
            COUNTROWS ( Table1 ),
            FILTER (
                Table1,
                'Table1'[Factor2014] < E2014
                    || 'Table1'[Factor2015] < E2015
                    || 'Table1'[Factor2016] < E2016
            )
    )

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    Best Regards!

    Dale

     

     

7 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    I think you are going to want to reshape your data.  Likely you will find it easier to deal with a single factor column, and a separate year column.  (You can use the Unpivot functionality in Edit Queries for this).

     

    I'm also skeptical that you are going to want comma separated values like that...

    • vmsouza30's avatar
      vmsouza30
      Helper I
      In this case I do not have to worry about separating the factor in date, here could be Factor1, factor2, etc.
      My problem is to have the reference index of a client and do the calculations, as explained above.
  • Anonymous's avatar
    Anonymous
    Not applicable

    I find having 3 columns for Factor pretty awkward, but... here are some ideas to get you started?

     

    Total Customers = COUNTROWS(MyTable)

    Total Factor := SUMX(MyTable, MyTable[Factor 2014] + MyTable[Factor 2015] + MyTable[Factor 2016])

    E Factor := CALCULATE([Total Factor], MyTable[Clientes] = "E")

    Low Customers := CALCULATE([Total Customers], FILTER(MyTable, [Total Factor] < [E Factor])

    High Customers := CALCULATE([Total Customers], FILTER(MyTable, [Total Factor] > [E Factor])

  • LairdLight's avatar
    LairdLight
    Frequent Visitor

    I agree with the other posts suggesting you reshape your data to have a single factor column and a year date dimension.

     

    Having said that I think you're after something like this, I created a table like yours called Factor...

     

    Then I created a new column called "Clientes Factor" which totals the three factor values for each client:

    Clientes Factor = CALCULATE(SUMX(factor, factor[Factor 2014]+factor[factor 2015]+factor[factor 2016]))

     

    Then I created a new measure called "Factor E Calc" which is Client Es factor total for comparison:

    Factor E Calc= calculate(SUMX(factor, factor[Factor 2014]+factor[factor 2015]+factor[factor 2016]), Factor[clientes]="E")

     

    Then to work around a circular dependency error I created a new measure called "Factor E" which is equal to the value I entered manually (copying the value calculated in "Factor E Calc):

    Factor E = 6.1

     

    Then I created a new column comparing "Clientes Factor" with "Factor E" I called it Factor Rating:

    Factor Rating = if(Factor[Clientes Factor]<Factor[Factor E], "Less", (if(Factor[Clientes Factor]=Factor[Factor E], "Same", "Higher")))

     

    Here's what the resulting table looks like:

    groupclientesFactor 2014Factor 2015Factor 2016Clientes FactorFactor Rating
    1A122.35.3Less
    1B1.53.41.36.2Higher
    1C2.311.75Less
    1D1.31.51.34.1Less
    2E1.722.46.1Same
    2F241.57.5Higher
    2G3.41.72.27.3Higher
    2H12.325.3Less
    3I1.51.32.45.2Less
    3J21.714.7Less
    3K42.42.48.8Higher
    3L1.74.728.4Higher

     

    I'm sure there's more elegant ways of achieving the same outcome but hopefully that helps :)

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    vmsouza30

     

    Hi,

     

    Maybe you could try these formulas.

     

     

    Above =
    VAR E2014 =
        CALCULATE (
            MIN ( Table1[Factor2014] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2015 =
        CALCULATE (
            MIN ( Table1[Factor2015] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2016 =
        CALCULATE (
            MIN ( Table1[Factor2016] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    RETURN
        CALCULATE (
            COUNTROWS ( Table1 ),
            FILTER (
                Table1,
                'Table1'[Factor2014] > E2014
                    || 'Table1'[Factor2015] > E2015
                    || 'Table1'[Factor2016] > E2016
            )
        )
    Below =
    VAR E2014 =
        CALCULATE (
            MIN ( Table1[Factor2014] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2015 =
        CALCULATE (
            MIN ( Table1[Factor2015] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    VAR E2016 =
        CALCULATE (
            MIN ( Table1[Factor2016] ),
            FILTER ( ALL ( Table1 ), 'Table1'[Clients] = "E" )
        )
    RETURN
        CALCULATE (
            COUNTROWS ( Table1 ),
            FILTER (
                Table1,
                'Table1'[Factor2014] < E2014
                    || 'Table1'[Factor2015] < E2015
                    || 'Table1'[Factor2016] < E2016
            )
    )

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    Best Regards!

    Dale