Search code examples
excelpowerbipowerquerydata-analysism

How to avoid redundancy when at least two queries share common steps?


I have five queries that share some steps. But for simplicity, lets consider only two queries.

As you can see below the two queries Q1 and Q2 share the first three steps and the last step.

My question : Is it possible to reference the two first three steps and the last step in two separate queries so I can call them in Q1 and Q2 ? Because now I have a problem. If for example I need to modify a common step or add/remove one, I need to do that for Q1 and Q2.

Q1

let
    Source = Csv.Document(File.Contents("C:\Users\VERBOSE\Desktop\file.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"En-têtes promus" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Personnalisée ajoutée" = Table.AddColumn(#"En-têtes promus", "new_col", each "###"),
    #"Lignes filtrées" = Table.SelectRows(#"Personnalisée ajoutée", each ([col3] = "1"))
in
    #"Lignes filtrées"

Q2

let
    Source = Csv.Document(File.Contents("C:\Users\VERBOSE\Desktop\file.csv"),[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"En-têtes promus" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Personnalisée ajoutée" = Table.AddColumn(#"En-têtes promus", "new_col", each "XYZ"),
    #"Lignes filtrées" = Table.SelectRows(#"Personnalisée ajoutée", each ([col3] = "1"))
in
    #"Lignes filtrées"

The csv file used in my example is this one :

col1,col2,col3
A,13/01/2023,1
B,14/01/2023,2
C,15/01/2023,3

Solution

  • You can achieve this in 2 ways. You can have base queries so your first two steps are in a base query, then right click the query and reference that query for your two output queries. Ensure enable load is unchecked on your base query. For the last step, you can abstract the common logic to a function and call the function in your two output queries.

    If you need details, let me know and I'll set up some screenshots.

    EDIT

    Create a blank query named myFunction and paste in the following:

    (x as table) =>
    
    let
        Source =  Table.SelectRows(x, each ([col3] = 1))
    in
        Source
    

    In your two output queries, simply call the function you created as follows:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"col1", type text}, {"col2", type datetime}, {"col3", Int64.Type}}),
        Custom1 = myFunction(#"Changed Type")
    in
        Custom1
    

    enter image description here