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
DonPepe
Helper II
Helper II

Clean up M code

Hello, 

 

I would like to know if you have another mean (more clean) to get the same result. And if not, how can I modify my code to be more understanble ? 

 

The purpose of the code is to filter the last week of the imported data (from sunday to saturday) 

 

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\TabData.xlsx"), null, true),
    Tableau1_Table = Source{[Item="Tableau1",Kind="Table"]}[Data],
    AddDateMaxDb = Table.AddColumn(Table.TransformColumnTypes(Tableau1_Table,{{"PLAATY Start 
           Date", type date}}), "Personnalisé", each 
           List.Max(Table.TransformColumnTypes(Tableau1_Table,{{"PLAATY Start Date", type 
           date}})[PLAATY Start Date])),
    AddDateL = Table.AddColumn(AddDateMaxDb, "Personnalisé.1", each 
           Date.AddDays(Date.AddWeeks(Date.StartOfWeek([Personnalisé]),-1),-1)),
    #"Personnalisée ajoutée2" = Table.AddColumn(AddDateL, "Personnalisé.2", each if [PLAATY 
           Start Date] >= Date.AddDays(Date.AddWeeks(Date.StartOfWeek([Personnalisé]),-1),-1) 
           and [PLAATY Start Date] <= 
           Date.AddDays(Date.AddWeeks(Date.StartOfWeek([Personnalisé]),-1),+5) then [PLAATY 
           Start Date] else "O"),
    #"Colonnes supprimées" = Table.RemoveColumns(Table.SelectRows(#"Personnalisée ajoutée2", 
           each ([Personnalisé.2] <> "O")),{"Personnalisé", "Personnalisé.1", 
           "Personnalisé.2"})
in
    #"Colonnes supprimées"

 

Thanks in advance ! 

 

Don 

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

let
    Source = Excel.Workbook(File.Contents("C:\Users\TabData.xlsx"), null, true),
    Tableau1_Table = Table.TransformColumnTypes(Source{[Item="Tableau1",Kind="Table"]}[Data],{{"PLAATY Start Date", type date}}),
    DateMax = List.Max(Tableau1_Table[PLAATY Start Date]),
    DateL = Date.AddDays(Date.AddWeeks(Date.StartOfWeek(DateMax),-1),-1),
    DateM = Date.AddDays(Date.AddWeeks(Date.StartOfWeek(DateMax),-1),+5),
    Custom1 = Table.SelectRows(Tableau1_Table,each [PLAATY Start Date] >=DateL and [PLAATY Start Date] <= DateM))
in
    Custom1

View solution in original post

3 REPLIES 3
wdx223_Daniel
Super User
Super User

let
    Source = Excel.Workbook(File.Contents("C:\Users\TabData.xlsx"), null, true),
    Tableau1_Table = Table.TransformColumnTypes(Source{[Item="Tableau1",Kind="Table"]}[Data],{{"PLAATY Start Date", type date}}),
    DateMax = List.Max(Tableau1_Table[PLAATY Start Date]),
    DateL = Date.AddDays(Date.AddWeeks(Date.StartOfWeek(DateMax),-1),-1),
    DateM = Date.AddDays(Date.AddWeeks(Date.StartOfWeek(DateMax),-1),+5),
    Custom1 = Table.SelectRows(Tableau1_Table,each [PLAATY Start Date] >=DateL and [PLAATY Start Date] <= DateM))
in
    Custom1

Wow, thanks a lot, I will learn from that. 

BA_Pete
Super User
Super User

Hi @DonPepe ,

 

Can you provide a copyable example of your source data please?

Make sure to remove any sensitive information before sharing.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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.

Top Solution Authors
Top Kudoed Authors