Forum Discussion

prat_daxer's avatar
prat_daxer
New Member
3 years ago
Solved

Assigning a column value based on other column

I have data something like this:

Catagoryvaluefinal value
A11
A21
B22
B32
B42

Basically number 1,2,3,4 are priority and if any atagory has prio 1 also 2 assign both to 1.

  • It sounds like you are just trying to get the min of each group /category is that right?

    You'll need an expression like

    GroupMin = calculate(min('Table'[Value]), ALLEXCEPT('Table','Table'[Cat]))
    Calculate will give you the min, the first parameter calculation, and the context is specified in the second paramter, AllExcept. In this case the context "all except" tells DAX to return all values in the table, ie, ignore all groupings per row, except the cat grouping.
     
    This gives:

     

    Pi

  • pi_eye's avatar
    pi_eye
    3 years ago

    The solution then is something like this:

    Extended Logic =
    var contains5= calculate(CONTAINS('Table','Table'[Value],5),ALLEXCEPT('Table','Table'[Category]))
    var contains6=calculate(CONTAINS('Table','Table'[Value],6),ALLEXCEPT('Table','Table'[Category]))
    return Switch (FIRSTNONBLANK( 'Table'[Category],0) ,
        "A",if(contains6,FIRSTNONBLANK('Table'[Value],0),5),
        "B",if(contains5,FIRSTNONBLANK('Table'[Value],0),6),
        "C",if(and(contains5,contains6),floor(5+rand()+0.25,1)))
     
    vars contains5 and contains6 = these evaluate within the context of category, if the values 5 and 6 exist, respectively.

    Switch Is similar to a case statement if you are familar with those - if you are not familiar think of it as an extended IF() statement where you can return multiple values depending on an input value.
     
    Here we are passing the category to the switch statement, and depending on which category it is, returning a different value.
    IF category is A, we look use contains 6 to determine what to return - either the value, or 5. Similar to B.
    for C we are checking both contains5 and contains6 and if that's the case, using rand() to assign 5's and 6's
     
    HTH 
    Pi

6 Replies

  • It sounds like you are just trying to get the min of each group /category is that right?

    You'll need an expression like

    GroupMin = calculate(min('Table'[Value]), ALLEXCEPT('Table','Table'[Cat]))
    Calculate will give you the min, the first parameter calculation, and the context is specified in the second paramter, AllExcept. In this case the context "all except" tells DAX to return all values in the table, ie, ignore all groupings per row, except the cat grouping.
     
    This gives:

     

    Pi

  •  

    Catagoryvaluefinal value
    A55
    A75
    B76
    B66
    C55
    C65
    C55
    C66

    Condition 1: Cat A --> Assign prio 5 only if 6 is not there against A.

    Condition 2: Cat B --> Assign prio 6 only if 5 is not present against B.

    Condition 3: Cat C --> If prio 5 and 6 both present against C, assign random 75% 5's to cat C and in rest 25%, 6's.

      • pi_eye's avatar
        pi_eye
        Resolver IV

        The solution then is something like this:

        Extended Logic =
        var contains5= calculate(CONTAINS('Table','Table'[Value],5),ALLEXCEPT('Table','Table'[Category]))
        var contains6=calculate(CONTAINS('Table','Table'[Value],6),ALLEXCEPT('Table','Table'[Category]))
        return Switch (FIRSTNONBLANK( 'Table'[Category],0) ,
            "A",if(contains6,FIRSTNONBLANK('Table'[Value],0),5),
            "B",if(contains5,FIRSTNONBLANK('Table'[Value],0),6),
            "C",if(and(contains5,contains6),floor(5+rand()+0.25,1)))
         
        vars contains5 and contains6 = these evaluate within the context of category, if the values 5 and 6 exist, respectively.

        Switch Is similar to a case statement if you are familar with those - if you are not familiar think of it as an extended IF() statement where you can return multiple values depending on an input value.
         
        Here we are passing the category to the switch statement, and depending on which category it is, returning a different value.
        IF category is A, we look use contains 6 to determine what to return - either the value, or 5. Similar to B.
        for C we are checking both contains5 and contains6 and if that's the case, using rand() to assign 5's and 6's
         
        HTH 
        Pi
  • Hi prat_daxer,

     

    Are you trying to do this with DAX (front end UI) or M query (data shaping and modelling)

     

    Pi