Forum Discussion

BInovice's avatar
BInovice
Frequent Visitor
4 years ago
Solved

Group data based on multiple conditions

All, I could use some help with this problem. I have a large table with "Mail type" and 'Delivery time" and I want to group the data or add a column retuning "Group" value based on the reference table below:

Mail typeDelivery time minDelivery time maxGroup
1st class14great
1st class57good
1st class8 bad
Priority13great
Priority46good
Priority7 bad
Express01great
Express23good
Express4 bad

 

What are my options?

 

Many thanks

  • BInovice So what are the rules? You could do something like:

    Group =
      SWITCH(TRUE(),
        [Mail type] = "1st class" && [Delivery time max] <=4, "great",
        [Mail type] = "1st class" && [Delivery time max] <=7, "good",
        [Mail type] = "1st class" && [Delivery time max] >7, "bad",
        ...
      )
        
  • BInovice  you can write a measure like this

    Measure =
    VAR _mail =
        MAX ( t1[Mail type] )
    VAR _time =
        MAX ( t1[Delivery time] )
    VAR rating =
        CALCULATE (
            MAX ( t2[Group] ),
            FILTER ( t2, _time >= [Delivery time min] && _time <= [Delivery time max] ),
            TREATAS ( { _mail }, t2[Mail type] )
        )
    RETURN
        IF ( ISBLANK ( rating ) = TRUE (), "bad", rating )

     

     

3 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    BInovice So what are the rules? You could do something like:

    Group =
      SWITCH(TRUE(),
        [Mail type] = "1st class" && [Delivery time max] <=4, "great",
        [Mail type] = "1st class" && [Delivery time max] <=7, "good",
        [Mail type] = "1st class" && [Delivery time max] >7, "bad",
        ...
      )
        
    • BInovice's avatar
      BInovice
      Frequent Visitor

      Greg_Deckler Thank you very much. It works great. I went for solution from smpa01 as that allows use to update the limits from separated table.

  • smpa01's avatar
    smpa01
    Community Champion

    BInovice  you can write a measure like this

    Measure =
    VAR _mail =
        MAX ( t1[Mail type] )
    VAR _time =
        MAX ( t1[Delivery time] )
    VAR rating =
        CALCULATE (
            MAX ( t2[Group] ),
            FILTER ( t2, _time >= [Delivery time min] && _time <= [Delivery time max] ),
            TREATAS ( { _mail }, t2[Mail type] )
        )
    RETURN
        IF ( ISBLANK ( rating ) = TRUE (), "bad", rating )