Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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).

1 ACCEPTED SOLUTION

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.
image.png

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

 

View solution in original post

12 REPLIES 12
HotChilli
Super User
Super User

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

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

 

What happened to the sick leave data?

Anonymous
Not applicable

I've just given an example set of data. Once I know how to treat two tables I'm sure I can work out how to treat three.

You can link the tables on Employee Number in Relationship View.

 

Create a Measure to ensure the employees with no leave still show up

Emp leave = SUM(EmployeeLeave[Annual Leave]) + 0 

Then pull the relevant fields on to a table visualisation

Anonymous
Not applicable

Thanks but that's not what I'm looking for. I need to create a date-based table that logs all activity against the individual. The key is to have every staff member represented against every month they are active (time beween employee start date and employee end date).

Hi @Anonymous 

 

The below M function can be used as invoke custom function on the staff data table, this will expand the table including every month of employment, after you will  need to merge with the second table on employee number and month 

(#"Start Date" as date, optional #"End Date" as date ) => let
    enddate = if #"End Date" = null then DateTime.Date( DateTime.LocalNow() ) else  #"End Date", 
    days = Duration.Days( enddate - #"Start Date" ),
    listDates = List.Dates( #"Start Date", days, #duration( 1, 0, 0, 0 ) ),
    firstDates = List.Select( listDates, each Date.Day( _ ) = 1 ),
    monthYear = List.Transform( firstDates, each Text.Start( Date.MonthName( _ ), 3 ) & "- " & Text.End( Number.ToText( Date.Year( _ ) ), 2  ) )
    
in
    monthYear

 

Best Regards,
Mariusz

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

Please feel free to connect with me.
Mariusz Repczynski

 

Anonymous
Not applicable

That looks perfect but I've never used M before. Is there any way to do this in DAX?

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.
image.png

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

 

Anonymous
Not applicable

This worked like a charm, thanks so much! I definitely wouldn't have got there without you.

Yes, that did go through my mind but the sample data doesn't seem to fit that requirement e.g. Janet Bloggs has leave in June and July 19 but has left in April 2018

Anonymous
Not applicable

Sorry, that was a mistake. It's not easy to give a complete dataset to work with as I would need to anonymise everything.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.