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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
GeekAlfPro
Helper V
Helper V

Cleaning dates issue

Hi there,

 

i've a file, i need to clean.

in my samples data below, i have to keep the last date (Justif - Date fin absence) by people, but only if the column (Regroupement) is the same, and if the date date are in the same periode.

e.g. the first 4 lines should be one line because the dates follow each other (09-11-2020 to 07-12-2020)

inversely, for the last 3 lines, i should have 2 lines (02-11-2020 to 04-11-2020) and (18-11-2020 to 24-11-2020)

 

 

MatriculeRegroupementNomPrénomN° SSIndexJustification (Code)Justification (Libellé)Date d'arrêt initial

(MAL et MALP)
Justif - Date début absenceJustif - Date fin absenceNb jours sur la période sélectionnéeNb jours total par arrêt
50444MALNesboJo2320299350233250MALMaladie09/11/202009/11/202015/11/202077
50444MALNesboJo2320299350233251MALMaladie16/11/202016/11/202022/11/202077
50444MALNesboJo2320299350233252MALMaladie23/11/202023/11/202029/11/202077
50444MALNesboJo2320299350233253MALPProlongation maladie23/11/202030/11/202007/12/202018
49999ATEccoUmberto2620526520262220TVLPProlongation ATnull23/10/202022/11/20202231
49999ATEccoUmberto2620526520262221TVLPProlongation ATnull23/11/202021/12/2020829
49555MALHerbertFrank2630626522002560MALMaladie02/11/202002/11/202006/11/202055
49555MALHerbertFrank2630626522002561MALPProlongation maladie02/11/202007/11/202013/11/202077
49555MALHerbertFrank2630626522002562MALPProlongation maladie02/11/202014/11/202028/11/20201515
49555MALHerbertFrank2630626522002563MALPProlongation maladie02/11/202029/11/202012/12/2020214
49333MALMishimaYukio2622226293332330MALMaladie02/11/202002/11/202004/11/202033
49333MALMishimaYukio2622226293332331MALMaladie18/11/202018/11/202020/11/202033
49333MALMishimaYukio2622226293332332MALPProlongation maladie18/11/202021/11/202024/11/202044

 

Thanks for your help !

 

 

1 ACCEPTED SOLUTION
AlB
Community Champion
Community Champion

@GeekAlfPro 

Yes, it´s aexactly the same as what was done in the thread I pointed to earlier. Place the following M code in a blank query to see the steps. The forst two steps are just to enter your sample data. #"Add Column" is the crucial step. It could be made into a function to make it more legible

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZRNb4MwDIb/ysS5EomdBDj2sGma1qmHbtJUcaAt2lD5kGj7/2dSBM7GxKBDsoOR4XHsN2y3nhZKKW/hrZbP5F/S066i9alxgCAgilALQARNT0SXuEry5JClzbPIl9KnTPE9kJoFgbV4MZEoB4jScAgPAG4nwgARkEOcILqdiNfENS3rusqr8iM5Z1V5VwzzUfCGB76ErhdkoeWriC6Klhty9/t9A34tdml9tiUYEBqMptcMALSD3bz9LMG+X17yvC1CDHbafgLlLLL8I7mHSbbj0I6gBWutu5Y/pnXDpLuHOimPFo3CNGgQArT5Tc18X27AhaatzcPK0XG73ICrHQflNr0ImFaEVHwCoXPIr25eGePSd8pwThupoBeClZJqq0DEfrTZ6TMrErp7vxyzVoN0GWjSwGZOFALvBVqbhx38tzm95QGIf8KOj97lSh7wvStrcfwF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Matricule = _t, Regroupement = _t, Nom = _t, Prénom = _t, #"N° SS" = _t, Index = _t, #"Justification (Code)" = _t, #"Justification (Libellé)" = _t, #"Date d'arrêt initial#(cr)#(lf)(MAL et MALP)" = _t, #"Justif - Date début absence" = _t, #"Justif - Date fin absence" = _t, #"Nb jours sur la période sélectionnée" = _t, #"Nb jours total par arrêt" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Matricule", Int64.Type}, {"Regroupement", type text}, {"Nom", type text}, {"Prénom", type text}, {"N° SS", type text}, {"Index", Int64.Type}, {"Justification (Code)", type text}, {"Justification (Libellé)", type text}, {"Date d'arrêt initial#(cr)#(lf)(MAL et MALP)", type date}, {"Justif - Date début absence", type date}, {"Justif - Date fin absence", type date}, {"Nb jours sur la période sélectionnée", Int64.Type}, {"Nb jours total par arrêt", Int64.Type}}),


    #"Grouped Rows" = Table.Group(#"Changed Type", {"Matricule"}, {{"Grouped", each _, type table [Matricule=nullable number, Regroupement=nullable text, Nom=nullable text, Prénom=nullable text, #"N° SS"=nullable text, Index=nullable number, #"Justification (Code)"=nullable text, #"Justification (Libellé)"=nullable text, #"Date d'arrêt initial#(cr)#(lf)(MAL et MALP)"=nullable date, #"Justif - Date début absence"=nullable date, #"Justif - Date fin absence"=nullable date, Nb jours sur la période sélectionnée=nullable number, Nb jours total par arrêt=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let inputT_ =[Grouped],
            colStart_ = inputT_[#"Justif - Date début absence"],
            colEnd_ = inputT_[#"Justif - Date fin absence"],
            S1_ = Table.AddIndexColumn(inputT_,"Index2",0),
            S2_ = Table.AddColumn(S1_, "Date1", each if try colEnd_{[Index2]-1}=Date.AddDays([#"Justif - Date début absence"],-1) otherwise false then null else [#"Justif - Date début absence"], type date),
            S3_ = Table.AddColumn(S2_, "Date2", each if try colStart_{[Index2]+1}=Date.AddDays([#"Justif - Date fin absence"], 1) otherwise false then null else [#"Justif - Date fin absence"], type date),
            #"Filled Up" = Table.FillUp(S3_,{"Date2"}),
            #"Filtered Rows" = Table.SelectRows(#"Filled Up", each [Date1] <> null)
        in
            #"Filtered Rows"),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Regroupement", "Nom", "Prénom", "N° SS", "Index", "Justification (Code)", "Justification (Libellé)", "Date d'arrêt initial#(cr)#(lf)(MAL et MALP)", "Justif - Date début absence", "Justif - Date fin absence", "Nb jours sur la période sélectionnée", "Nb jours total par arrêt", "Index2", "Date1", "Date2"}, {"Regroupement", "Nom", "Prénom", "N° SS", "Index", "Justification (Code)", "Justification (Libellé)", "Date d'arrêt initial#(cr)#(lf)(MAL et MALP)", "Justif - Date début absence", "Justif - Date fin absence", "Nb jours sur la période sélectionnée", "Nb jours total par arrêt", "Index2", "Date1", "Date2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Grouped", "Index2", "Justif - Date début absence", "Justif - Date fin absence", "Nb jours sur la période sélectionnée", "Nb jours total par arrêt"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Date1", "Justif - Date début absence"}, {"Date2", "Justif - Date fin absence"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Justif - Date début absence", type date}, {"Justif - Date fin absence", type date}})
in
    #"Changed Type1"

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

View solution in original post

5 REPLIES 5
AlB
Community Champion
Community Champion

@GeekAlfPro 

See it all at work in the attached file.

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

AlB
Community Champion
Community Champion

@GeekAlfPro 

Yes, it´s aexactly the same as what was done in the thread I pointed to earlier. Place the following M code in a blank query to see the steps. The forst two steps are just to enter your sample data. #"Add Column" is the crucial step. It could be made into a function to make it more legible

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZRNb4MwDIb/ysS5EomdBDj2sGma1qmHbtJUcaAt2lD5kGj7/2dSBM7GxKBDsoOR4XHsN2y3nhZKKW/hrZbP5F/S066i9alxgCAgilALQARNT0SXuEry5JClzbPIl9KnTPE9kJoFgbV4MZEoB4jScAgPAG4nwgARkEOcILqdiNfENS3rusqr8iM5Z1V5VwzzUfCGB76ErhdkoeWriC6Klhty9/t9A34tdml9tiUYEBqMptcMALSD3bz9LMG+X17yvC1CDHbafgLlLLL8I7mHSbbj0I6gBWutu5Y/pnXDpLuHOimPFo3CNGgQArT5Tc18X27AhaatzcPK0XG73ICrHQflNr0ImFaEVHwCoXPIr25eGePSd8pwThupoBeClZJqq0DEfrTZ6TMrErp7vxyzVoN0GWjSwGZOFALvBVqbhx38tzm95QGIf8KOj97lSh7wvStrcfwF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Matricule = _t, Regroupement = _t, Nom = _t, Prénom = _t, #"N° SS" = _t, Index = _t, #"Justification (Code)" = _t, #"Justification (Libellé)" = _t, #"Date d'arrêt initial#(cr)#(lf)(MAL et MALP)" = _t, #"Justif - Date début absence" = _t, #"Justif - Date fin absence" = _t, #"Nb jours sur la période sélectionnée" = _t, #"Nb jours total par arrêt" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Matricule", Int64.Type}, {"Regroupement", type text}, {"Nom", type text}, {"Prénom", type text}, {"N° SS", type text}, {"Index", Int64.Type}, {"Justification (Code)", type text}, {"Justification (Libellé)", type text}, {"Date d'arrêt initial#(cr)#(lf)(MAL et MALP)", type date}, {"Justif - Date début absence", type date}, {"Justif - Date fin absence", type date}, {"Nb jours sur la période sélectionnée", Int64.Type}, {"Nb jours total par arrêt", Int64.Type}}),


    #"Grouped Rows" = Table.Group(#"Changed Type", {"Matricule"}, {{"Grouped", each _, type table [Matricule=nullable number, Regroupement=nullable text, Nom=nullable text, Prénom=nullable text, #"N° SS"=nullable text, Index=nullable number, #"Justification (Code)"=nullable text, #"Justification (Libellé)"=nullable text, #"Date d'arrêt initial#(cr)#(lf)(MAL et MALP)"=nullable date, #"Justif - Date début absence"=nullable date, #"Justif - Date fin absence"=nullable date, Nb jours sur la période sélectionnée=nullable number, Nb jours total par arrêt=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let inputT_ =[Grouped],
            colStart_ = inputT_[#"Justif - Date début absence"],
            colEnd_ = inputT_[#"Justif - Date fin absence"],
            S1_ = Table.AddIndexColumn(inputT_,"Index2",0),
            S2_ = Table.AddColumn(S1_, "Date1", each if try colEnd_{[Index2]-1}=Date.AddDays([#"Justif - Date début absence"],-1) otherwise false then null else [#"Justif - Date début absence"], type date),
            S3_ = Table.AddColumn(S2_, "Date2", each if try colStart_{[Index2]+1}=Date.AddDays([#"Justif - Date fin absence"], 1) otherwise false then null else [#"Justif - Date fin absence"], type date),
            #"Filled Up" = Table.FillUp(S3_,{"Date2"}),
            #"Filtered Rows" = Table.SelectRows(#"Filled Up", each [Date1] <> null)
        in
            #"Filtered Rows"),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Regroupement", "Nom", "Prénom", "N° SS", "Index", "Justification (Code)", "Justification (Libellé)", "Date d'arrêt initial#(cr)#(lf)(MAL et MALP)", "Justif - Date début absence", "Justif - Date fin absence", "Nb jours sur la période sélectionnée", "Nb jours total par arrêt", "Index2", "Date1", "Date2"}, {"Regroupement", "Nom", "Prénom", "N° SS", "Index", "Justification (Code)", "Justification (Libellé)", "Date d'arrêt initial#(cr)#(lf)(MAL et MALP)", "Justif - Date début absence", "Justif - Date fin absence", "Nb jours sur la période sélectionnée", "Nb jours total par arrêt", "Index2", "Date1", "Date2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Grouped", "Index2", "Justif - Date début absence", "Justif - Date fin absence", "Nb jours sur la période sélectionnée", "Nb jours total par arrêt"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Date1", "Justif - Date début absence"}, {"Date2", "Justif - Date fin absence"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Justif - Date début absence", type date}, {"Justif - Date fin absence", type date}})
in
    #"Changed Type1"

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

Hi @AlB 

 

thanks a lot, the result is exactly what i need !

i wouldn't have found it on my own.

thanks again and have a good day

AlB
Community Champion
Community Champion

Hi @GeekAlfPro 

Can you share that table in an excel file? I cannot quite copy it correctly, with the aligned column names. And please include also a second table with the expected result.

You have to share the URL to the file hosted elsewhere: Dropbox, Onedrive... or just upload the file to a site like tinyupload.com (no sign-up required).

We recently solved something that is similar. Have a look:

https://community.powerbi.com/t5/Power-Query/How-to-merge-two-rows-when-date-range-is-in-continuatio...

 

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

Hello @AlB 

 

here is the file 

You will find 2 tabs with the expected result !

 

thanks for your time

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors