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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mhrkhader
Regular Visitor

group Leave Dates for Emp By Date Sequence

Dear Team,

 

Greeting.

 

i have excel sheet with trhe following Data

 

EmpID      LeaveDate  

E001         12 Mar 2019

E001         13 Mar 2019

E001         15 Mar 2019

E001         17 Apr 2019

E001         18 Apr 2019

E002       12 Mar 2019

E002       15 Mar 2019

E002        22 May 2019

E002        23  May 2019

 

but the result we want in PowerQuery is

EmpID      LeaveDate          Leave To
E001         12 Mar 2019      13 Mar 2019
E001         15 Mar 2019      15 Mar 2019
E001         17 Apr 2019      18 Apri 2019
E002       12 Mar 2019       12 Mar 2019
E002       15 Mar 2019       15 Mar 2019
E002        22 May 2019      23  May 2019

 

how to achive this based on dates Seq using PowerQuery? if the Dates in a Seq for a Particular Emp, LEave Start Date and End Date shoud be assumed that the Leave Dates between Start and End and anything between

 

thanks

 

regards

Maher

1 ACCEPTED SOLUTION

Thanks @mhrkhader ,

that's clear now and makes sense.

I've created an Index for the sequences with List.Accumulate:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTW9U0sUorVATJNEUwzBNMSzjQyQjAR2oxMEExzBBOhzdgAwowFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    TransformToList = #"Changed Type"[Date],
    AddSequenceIndex = List.Accumulate(
        TransformToList, 
        {[Value = List.First(TransformToList), Index = 0]}, 
        (state, current) => if current - List.Last(state)[Value] = #duration(1,0,0,0) 
                                then state & { [Value = current, Index = List.Last(state)[Index] ] } 
                                else state & { [Value = current, Index = List.Last(state)[Index] + 1 ] }
    ),
    #"Converted to Table" = Table.FromList(AddSequenceIndex, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Value", "Index"}, {"Value", "Index"}),
    #"Grouped Rows" = Table.Group(#"Expanded Column1", {"Index"}, {{"Partition", each _, type table [Value=date, Index=number]}}, GroupKind.Local),
    SkipSeed = Table.SelectRows(#"Grouped Rows", each ([Index] <> 0)),
    FirstElementAsStart = Table.AddColumn(SkipSeed, "Leave Date", each Table.First([Partition])[Value]),
    LastElementAsEnd = Table.AddColumn(FirstElementAsStart, "Leave To", each Table.Last([Partition])[Value]),
    Cleanup = Table.RemoveColumns(LastElementAsEnd,{"Partition"})
in
    Cleanup

 

Paste that into the advanced editor or check the file enclosed:

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

7 REPLIES 7
Greg_Deckler
Super User
Super User

@ImkeF  generally has tricks for this kind of thing.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi @mhrkhader  

I don't understand your explanation of the pattern to be used here. Please try to answer my questions below:

image.png

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

as your question you asked why we are not creating from 12 to 15 March, but the emp apply on leave on 12 and then resume work on 13 and 14 and then apply for leave on 15 march. so in this scenario i will make 2 entries. one for 12 March and one for 15 March

Thanks @mhrkhader ,

that's clear now and makes sense.

I've created an Index for the sequences with List.Accumulate:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTW9U0sUorVATJNEUwzBNMSzjQyQjAR2oxMEExzBBOhzdgAwowFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    TransformToList = #"Changed Type"[Date],
    AddSequenceIndex = List.Accumulate(
        TransformToList, 
        {[Value = List.First(TransformToList), Index = 0]}, 
        (state, current) => if current - List.Last(state)[Value] = #duration(1,0,0,0) 
                                then state & { [Value = current, Index = List.Last(state)[Index] ] } 
                                else state & { [Value = current, Index = List.Last(state)[Index] + 1 ] }
    ),
    #"Converted to Table" = Table.FromList(AddSequenceIndex, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Value", "Index"}, {"Value", "Index"}),
    #"Grouped Rows" = Table.Group(#"Expanded Column1", {"Index"}, {{"Partition", each _, type table [Value=date, Index=number]}}, GroupKind.Local),
    SkipSeed = Table.SelectRows(#"Grouped Rows", each ([Index] <> 0)),
    FirstElementAsStart = Table.AddColumn(SkipSeed, "Leave Date", each Table.First([Partition])[Value]),
    LastElementAsEnd = Table.AddColumn(FirstElementAsStart, "Leave To", each Table.Last([Partition])[Value]),
    Cleanup = Table.RemoveColumns(LastElementAsEnd,{"Partition"})
in
    Cleanup

 

Paste that into the advanced editor or check the file enclosed:

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

hi imkef,

 

i tried but it is not working.

 

i attached the file for your reference. 

https://we.tl/t-yje6M3rBty

 

i have the following code, where to past and what changes do i have to make for current code so it will work.

 

the code is 

***************

let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(Source, {"Code"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns1",{{"Attribute", "Date"}}),
#"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each ([Value] = "AB")),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Code", Order.Ascending}, {"Date", Order.Ascending}}),
#"Changed Type with Locale" = Table.TransformColumnTypes(#"Sorted Rows", {{"Date", type date}}, "en-GB"),
#"Renamed Columns1" = Table.RenameColumns(#"Changed Type with Locale",{{"Date", "From Date"}}),
#"Removed Columns" = Table.RemoveColumns(#"Renamed Columns1",{"Value"})
in
#"Removed Columns"

******************

 

mainly now i have 2 Columns (Code, Leave Date)

thanks

Hi @mhrkhader 

sorry, forgot to mention that you have to transform this into a function that you have to apply on each emplyees-figures. Please check workbook enclosed.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi Imkef,

 

Greetings

 

lets take this example

emp ID is E001 apply for leave for Dates 13,15,16,19,22,23,24,27,29,30 March

the report should show this

EIDDate
E00113 march
E00115 March
E00116 March
E00119 March
E00122 March
E00123 March
E00124 March
E00127 March
E00129 March
E00130 March

 

the report should show Dates from Start to End if they are in Seqence with increment of 1 but if there is a Gap of more than 1 day then we should split the imtervals. her is the result

EIDStart DateEnd DateNote
E00113 March 201913 March 2019her becase this date is one day leave without any consective date after we will make this date same in both columns
E00115 March 201916 March 2019 
E00119 March 201919 March 2019 
E00122 March 201924 March 2019 
E00127 March 201927 March 2019 
E00129 March 201930 March 2019 

thanks for your support Imkef and thanks for all who replied. please check and let me know.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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