Forum Discussion

madhav2020's avatar
madhav2020
Frequent Visitor
6 years ago
Solved

DAX query to filter between two dates for a given crtiteria

Hey Folks.

 

Good day !

 

Need help with a query for below mentioned criteria.

 

The requirement is the count on promotion to be calculated based on the selection between two different timelines from the user. (it may be for one month or quarter,etc..)

 

Employee Promotion table is as mentioned below. 

 

Emp No

Job Level

Period

123

1

1-Mar-20

456

2

1-Mar-20

654

1

1-Mar-20

987

3

1-Mar-20

223

5

1-Mar-20

123

1

1-Mar-20

456

3

1-Mar-20

654

2

1-Mar-20

987

3

1-Mar-20

223

6

1-Mar-20

123

2

1-Jun-20

456

3

1-Jun-20

654

2

1-Jun-20

987

3

1-Jun-20

223

5

1-Jun-20

123

2

1-Jul-20

456

4

1-Jul-20

654

2

1-Jul-20

987

3

1-Jul-20

223

6

1-Jul-20

 

Expected output is as follows:

 

 

Emp No

Job Level

Period

Output

123

1

1-Mar-20

 

456

2

1-Mar-20

 

654

1

1-Mar-20

 

987

3

1-Mar-20

 

223

5

1-Mar-20

 

123

1

1-Mar-20

No Change

456

3

1-Mar-20

Promoted

654

2

1-Mar-20

No Change

987

3

1-Mar-20

No Change

223

6

1-Mar-20

Promoted

123

2

1-Jun-20

Promoted

456

3

1-Jun-20

No Change

654

2

1-Jun-20

No Change

987

3

1-Jun-20

No Change

223

5

1-Jun-20

No Change

123

2

1-Jul-20

No Change

456

4

1-Jul-20

Promoted

654

2

1-Jul-20

No Change

987

3

1-Jul-20

No Change

223

6

1-Jul-20

Promoted

 

 

Check the above sample screen shot for requirement in power bi.

Promotion or not to be calculated based on the selection of two filters (Dates) as available in the above snapshot

  • Anonymous's avatar
    Anonymous
    6 years ago

    is this what you expect?

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc8xDoAgDEDRu3SGhFZAvYJXIAzu6GDi/UUdmlpkYPkhfW1KgDSAAazPod3Ww5KDbBL4EGsj1WPwzf/zNNY2qE7P/KB639VzXlfv03fjj3vPQbucu2ZFZlVkRkXmW0WWZJGk/2ZJljZZ9Jmc8wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Emp No" = _t, #"Job Level" = _t, Period = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp No", Int64.Type}, {"Job Level", Int64.Type}, {"Period", type date}},"en-US"),
        change=(tab)=>
        let
        idx=Table.AddIndexColumn(tab,"i",0,1),    
        chg=Table.AddColumn(idx, "chg", each  try if [Job Level]>idx[Job Level]{[i]-1} then "Promoted" else "No Change" otherwise null) 
        in chg,
    
    
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Emp No"}, {{"grp", each change(_)}}),
        #"Expanded grp" = Table.ExpandTableColumn(#"Grouped Rows", "grp", {"Job Level", "Period", "chg"}, {"Job Level", "Period", "chg"})
    in
        #"Expanded grp"

     

  • Hi madhav2020 ,

     

    If you want to implement it using DAX, you could try like this:

     

    Create columns:

     

    Add an index column in Query Editor

    rank =
    RANKX (
        FILTER (
            'Table',
            'Table'[Emp No] = EARLIER ( 'Table'[Emp No] )
                && 'Table'[Index] <= EARLIER ( 'Table'[Index] )
        ),
        'Table'[Index],
        ,
        ASC,
        DENSE
    )
    

     

    Promotion =
    VAR min_ =
        MAXX (
            FILTER (
                'Table',
                'Table'[Emp No] = EARLIER ( 'Table'[Emp No] )
                    && 'Table'[rank] < EARLIER ( 'Table'[rank] )
            ),
            'Table'[rank]
        )
    VAR joblevel =
        CALCULATE (
            MAX ( 'Table'[Job Level] ),
            FILTER (
                'Table',
                'Table'[Emp No] = EARLIER ( 'Table'[Emp No] )
                    && 'Table'[rank] = min_
            )
        )
    RETURN
        IF (
            'Table'[rank] > 1,
            IF ( 'Table'[Job Level] > joblevel, "Promoted", "No Change" ),
            "NULL"
        )
    

     

     

    Create a new unconnected table and use its column as a between slicer.

     

    Date = CALENDAR(MIN('Table'[Period]),MAX('Table'[Period]))

     

     

    Create a measure to count Promotion.

     

    count Promotion =
    COUNTROWS (
        FILTER (
            'Table',
            'Table'[Promotion] = "Promoted"
                && 'Table'[Period] >= MIN ( 'Date'[Date] )
                && 'Table'[Period] <= MAX ( 'Date'[Date] )
        )
    )
    

     

     

     

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    is this what you expect?

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc8xDoAgDEDRu3SGhFZAvYJXIAzu6GDi/UUdmlpkYPkhfW1KgDSAAazPod3Ww5KDbBL4EGsj1WPwzf/zNNY2qE7P/KB639VzXlfv03fjj3vPQbucu2ZFZlVkRkXmW0WWZJGk/2ZJljZZ9Jmc8wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Emp No" = _t, #"Job Level" = _t, Period = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp No", Int64.Type}, {"Job Level", Int64.Type}, {"Period", type date}},"en-US"),
        change=(tab)=>
        let
        idx=Table.AddIndexColumn(tab,"i",0,1),    
        chg=Table.AddColumn(idx, "chg", each  try if [Job Level]>idx[Job Level]{[i]-1} then "Promoted" else "No Change" otherwise null) 
        in chg,
    
    
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Emp No"}, {{"grp", each change(_)}}),
        #"Expanded grp" = Table.ExpandTableColumn(#"Grouped Rows", "grp", {"Job Level", "Period", "chg"}, {"Job Level", "Period", "chg"})
    in
        #"Expanded grp"

     

    • polymathy2017's avatar
      polymathy2017
      Icon for Advocate III rankAdvocate III

      Of course not. The poster specifically states DAX query in the subject. This is not a DAX query and therefore does not answer the question.

  • v-xuding-msft's avatar
    v-xuding-msft
    Icon for Community Support rankCommunity Support

    Hi madhav2020 ,

     

    If you want to implement it using DAX, you could try like this:

     

    Create columns:

     

    Add an index column in Query Editor

    rank =
    RANKX (
        FILTER (
            'Table',
            'Table'[Emp No] = EARLIER ( 'Table'[Emp No] )
                && 'Table'[Index] <= EARLIER ( 'Table'[Index] )
        ),
        'Table'[Index],
        ,
        ASC,
        DENSE
    )
    

     

    Promotion =
    VAR min_ =
        MAXX (
            FILTER (
                'Table',
                'Table'[Emp No] = EARLIER ( 'Table'[Emp No] )
                    && 'Table'[rank] < EARLIER ( 'Table'[rank] )
            ),
            'Table'[rank]
        )
    VAR joblevel =
        CALCULATE (
            MAX ( 'Table'[Job Level] ),
            FILTER (
                'Table',
                'Table'[Emp No] = EARLIER ( 'Table'[Emp No] )
                    && 'Table'[rank] = min_
            )
        )
    RETURN
        IF (
            'Table'[rank] > 1,
            IF ( 'Table'[Job Level] > joblevel, "Promoted", "No Change" ),
            "NULL"
        )
    

     

     

    Create a new unconnected table and use its column as a between slicer.

     

    Date = CALENDAR(MIN('Table'[Period]),MAX('Table'[Period]))

     

     

    Create a measure to count Promotion.

     

    count Promotion =
    COUNTROWS (
        FILTER (
            'Table',
            'Table'[Promotion] = "Promoted"
                && 'Table'[Period] >= MIN ( 'Date'[Date] )
                && 'Table'[Period] <= MAX ( 'Date'[Date] )
        )
    )