Forum Discussion

Etienne_1995's avatar
Etienne_1995
Frequent Visitor
3 years ago
Solved

converting DAX formula into M

Hi,   So this is my first post in this kind of community, since I'm really stuck. I'm quit new to Power Query Editor and need to convert two measures. ðŸ˜…   The goal is to create a new column in t...
  • v-yanjiang-msft's avatar
    3 years ago

    Hi Etienne_1995,

    According to your formula, I create a sample and get the result:

    If you want to perform the result in M language, here's my solution. Add a custom column.

    let 
    price_current_year =List.Sum(Table.SelectRows(#"Changed Type",(x)=>Date.Year(x[ERBK_Date])=Date.Year(Date.From(DateTime.LocalNow())))[ERBP_price_EW]),
    quantity_current_year =List.Sum(Table.SelectRows(#"Changed Type",(x)=>Date.Year(x[ERBK_Date])=Date.Year(Date.From(DateTime.LocalNow())))[ERBP_quantity])
    in 
    price_current_year/quantity_current_year

    Result:

    Here's the whole M syntax, you can copy-paste in a blank query to see the details.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdBLCsMwFEPRvXgcqCV/Yq8lZP/baPvkhlQtxpOTYNA9jsRMPF4nbel9W07ntpShNC2hxbSGVtMW2kx7aDfd/74wQofpDJ2myJqR3dc83wcNxPdCfmqUeOvm6/fqriJo7mqC7q4q2N3VBcNdZTDd1YbZXXUI99WH/PmgxbwWn08=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ERBK_Date = _t, ERBP_quantity = _t, ERBP_price_EW = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ERBK_Date", type date}, {"ERBP_quantity", Int64.Type}, {"ERBP_price_EW", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let price_current_year =List.Sum(Table.SelectRows(#"Changed Type",(x)=>Date.Year(x[ERBK_Date])=Date.Year(Date.From(DateTime.LocalNow())))[ERBP_price_EW]),quantity_current_year =List.Sum(Table.SelectRows(#"Changed Type",(x)=>Date.Year(x[ERBK_Date])=Date.Year(Date.From(DateTime.LocalNow())))[ERBP_quantity])in price_current_year/quantity_current_year)
    in
        #"Added Custom"

    I also attach my sample below for your reference.

     

    Best Regards,
    Community Support Team _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.