Forum Discussion

BarbaraK's avatar
BarbaraK
New Member
6 years ago
Solved

DAX Calculated Column needed in Query for join

I have query data "Weekly Sales" that looks a bit like this (changed for confidentiality):

 

CodeDescQtyIndex
 Debtor: XYZ Debtor 1
1010Product 1122
1020Product 233
1030Product 344
 Debtor: PRT Debtor 5
1010Product 1446
1020Product 2667
1030Product 338

 

Using this DAX:

Location = VAR a = 'Weekly Sales'[Index]
RETURN
CALCULATE (MAX ( 'Weekly Sales'[Desc] ),FILTER (ALL ( 'Weekly Sales' ),'Weekly Sales'[Index]= CALCULATE (MAX ( 'Weekly Sales'[Index] ),FILTER ( ALL ( 'Weekly Sales' ), 'Weekly Sales'[Index] <= a && LEFT('Weekly Sales'[Desc],6) = "Debtor" ))))

 

I get a calculated column that looks like this:

LocationCodeDescQtyIndex
Debtor: XYZ Debtor Debtor: XYZ Debtor 1
Debtor: XYZ Debtor1010Product 1122
Debtor: XYZ Debtor1020Product 233
Debtor: XYZ Debtor1030Product 344
Debtor: PRT Debtor Debtor: PRT Debtor 5
Debtor: PRT Debtor1010Product 1446
Debtor: PRT Debtor1020Product 2667
Debtor: PRT Debtor1030Product 338

 

This is exactly what I need but I need it in the query not on the destop so that I can use it in a query join.  Any help on how I can do this in query M or in the query editor would be very appreciated!

  • BarbaraK in query editor start a blank query and paste the following code and you will see the step on how to create one

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRcklNKskvslKIiIxSgLCBgkBkqBSrE61kaGBoAOQEFOWnlCaXKBiCJIyAhBFU1ghZFiRhDMYQSWNkSZCECRiDJJEsDggKQbHYFJfFJiDtZrgsNjMDEua4bAZhC6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Code = _t, Desc = _t, Qty = _t, Index = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", Int64.Type}, {"Desc", type text}, {"Qty", Int64.Type}, {"Index", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Location", each if [Code] = null then [Desc] else null),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"Location"})
    in
        #"Filled Down"

     

5 Replies

  • BarbaraK in query editor start a blank query and paste the following code and you will see the step on how to create one

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRcklNKskvslKIiIxSgLCBgkBkqBSrE61kaGBoAOQEFOWnlCaXKBiCJIyAhBFU1ghZFiRhDMYQSWNkSZCECRiDJJEsDggKQbHYFJfFJiDtZrgsNjMDEua4bAZhC6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Code = _t, Desc = _t, Qty = _t, Index = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", Int64.Type}, {"Desc", type text}, {"Qty", Int64.Type}, {"Index", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Location", each if [Code] = null then [Desc] else null),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"Location"})
    in
        #"Filled Down"

     

    • BarbaraK's avatar
      BarbaraK
      New Member

      At the risk of sounding daft:  This is my screen ATM and I am not really sure where to from here?

      • parry2k's avatar
        parry2k
        Super User

        BarbaraK right click on query1, and in pop up menu there is an option advanced editor , choose that and that is where you paste the code, make sure remove existing code 

    • BarbaraK's avatar
      BarbaraK
      New Member

      Oh that is beautiful.  I'm pretty sure I've got it from here. 🙂