Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Creating a new row with IF query in DAX

Hello,

 

I'm working with HR data and need to create a row to represent staff activity undertaken each by month. For example: 

 

MonthNameSick Leave in MonthAnnual Leave in Month
AprilJoe10
MayJoe02
JuneJoe11
MayPaul20
JunePaul11
JuneJon00

 

I have a list of all staff, their start date and end date, and all the activity data with relevant dates. One of the solutions I thought might work, but doesn't, is to create an IF query along the lines of:

 

In Month = IF(AND([Employee Start Date]<DATE(2019,04,01), [Leavers Date]>DATE(2019,04,30)), "April 2018",
IF(AND([Employee Start Date]<DATE(2019,05,01), [Leavers Date]>DATE(2019,05,31)), "May 2018",
IF(AND([Employee Start Date]<DATE(2019,06,01), [Leavers Date]>DATE(2019,06,30)), "June 2018",
IF(AND([Employee Start Date]<DATE(2019,07,01), [Leavers Date]>DATE(2019,07,31)), "July 2018",
IF(AND([Employee Start Date]<DATE(2019,08,01), [Leavers Date]>DATE(2019,08,31)), "August 2018",
IF(AND([Employee Start Date]<DATE(2019,09,01), [Leavers Date]>DATE(2019,08,30)), "September 2018",
BLANK()))))))

However this only attribites the first relevant result (eg Joe would be April 2018, Paul would be May 2018 and Joe would by June 2018). Can anyone think of a solution? Essentially I want to write a code that creates a new line and duplicates all other information (eg name).

  • Mariusz's avatar
    Mariusz
    7 years ago

    Hi Anonymous 

     

    1. In Query Editor go to Transform > New Query > New Source > Blank Query,

    2. Go to advanced editor and paste the code and rename the Query1 to ExpandMonths.

    3. When in your staff data table go to Add Column > General > Invoke Custom Function.
    4. Make sure you set everything as on the screenshot below.

    5. this will add new column "ExpandMonths", all you need to do now is click the two arrows an select expand to new rows.

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

    Please feel free to connect with me.
    Mariusz Repczynski

     

12 Replies

  • HotChilli's avatar
    HotChilli
    Icon for Community Champion rankCommunity Champion

    Can you post some sample data please and someone will help you out?

    • Anonymous's avatar
      Anonymous
      Not applicable

      I have two sets of data, the staff data:

       

      Employee Full NameEmployee NumberEmployee Start DateEmployee End Date
      Bloggs, Joe1111101/04/2018 01/01/4721 
      Bloggs, Janet2222215/01/2018 15/04/2018 
      Bloggs, Jane3333330/08/2015 30/02/2019  
      Bloggs, Jim4444405/02/2017 01/01/4721 

       

      And the annual leave data, NB this only reports if someone has taken annual leave, it does not include all staff:

       

      Employee Full NameEmployee NumberAnnual LeaveMonth
      Bloggs, Joe111111Jun-19
      Bloggs, Janet222221.5Jun-19
      Bloggs, Jim444446Jun-19
      Bloggs, Janet222226Jul-19
      Bloggs, Jane333335Jul-19
      Bloggs, Jim4444414Jul-19

       

      I need to create a table like this:

       

      Active MonthEmployee NumberEmployee Start DateEmployee End DateAnnual Leave
      Jun-191111101/04/2018 01/01/4721 1
      Jun-192222215/01/201815/04/2018 1.5
      Jun-193333330/08/201530/02/2019  0
      Jun-194444405/02/2017 01/01/4721 6
      Jul-191111101/04/201801/01/4721 0
      Jul-192222215/01/201815/04/2018 6
      Jul-193333330/08/201530/02/2019  5
      Jul-194444405/02/201701/01/472114