Search code examples
excelpowerbipowerquerypowerbi-desktopm

Calculating MTD QTD and YTD in power editor/query


I have 3 dax codes, but i would like to see these calculations in the power editor. Would this be possible? many thanks in advance.

  MTD = 
    CALCULATE(
        [measure],
        DATESMTD('Calendar'[Date])
    )
    QTD = 
    CALCULATE(
        [measure],
        DATESQTD('Calendar'[Date])
    )
    
    YTD = 
    CALCULATE(
        [measure],
        DATESYTD('Calendar'[Date],  "6-30")
    )

 

Solution

  • Here you go.

    enter image description here

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdk7ah1RFETRuSg2WFW3v2Mxnv803B1YIJ0FL9rZoit43PPnz8dnfj+/fnb7+PWRz4+/v55WtIW2oe1oB9qJdqHds+UTDY7AETgCR+AIHIEjcASOwlE4CkfhKByFo3AUjsJROBYcS7t6WrG10RbahrajHWgn2oV2z/ZubTQ4AkfgCByBI3AEjsAROApH4SgchaNwFI7CUTgKR+F4N7SwodEW2oa2ox1oJ9qFds/2bmg0OAJH4AgcgSNwBI7AETgKR+EoHIWjcBSOwlE4CkfhWHAs7eppG7Y22kLb0Ha0A+1Eu9Du2d6tjQZH4AgcgSNwBI7AETgCR+EoHIWjcBSOwlE4CkfhKBwLjndXO3Y12kLb0Ha0A+1Eu9Du2d5djQZH4AgcgSNwBI7AETgCR+EoHIWjcBSOwlE4CkfhKBwLjqVdPe3A1kZbaBvajnagnWgX2j3bu7XR4AgcgSNwBI7AETgCR+AoHIWjcBSOwlE4CkfhKByFY8Hx7urErkZbaBvajnagnWgX2j3bu6vR4AgcgSNwBI7AETgCR+AoHIWjcBSOwlE4CkfhKByFY8GxtKunXdjaaAttQ9vRDrQT7UK7Z3u3NhocgSNwBI7AETgCR+AIHIWjcBSOwlE4CkfhKByFo3AsOJZ29bQbWxttoW1oO9qBdqJdaPds79ZGgyNwBI7AETgCR+AIHIGjcBSOwlE4CkfhKByFo3AUjgXHs6vnM41dzbbQNrQd7UA70S60e7YnzAZH4AgcgSNwBI7AETgCR+EoHIWjcBSOwlE4CkfhKBwLjqVdPQ1v+rMttA1tRzvQTrQL7Z7t3Rre9GeDI3AEjsAROAJH4AgchaNwFI7CUTgKR+EoHIWjcCw43l3h/X62hbah7WgH2ol2od2zvbvC+/1scASOwBE4AkfgCByBo3AUjsJROApH4SgchaNwFI4Fx9Ku/t+P9u//w0ZbaBvajnagnWgX2j3b111y/7a12eAIHIEjcASOwBE4AkfhKByFo3AUjsJROApH4SgcC46lXf2/g/3Y2mgLbUPb0Q60E+1Cu2f7ukv+2NpocASOwBE4AkfgCByBo3D0p+PvPw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Amount = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Amount", Int64.Type}}),
        t = Table.Buffer( #"Changed Type"),
        MTD = Table.AddColumn(t, "MTD", each let a  = Table.SelectRows(t, (x)=> x[Date] <= [Date] and Date.Month(x[Date]) = Date.Month([Date]) and Date.Year(x[Date]) = Date.Year([Date]) ),
    b = List.Sum(a[Amount])
    in b),
        QTD = Table.AddColumn(MTD, "QTD", each let a  = Table.SelectRows(t, (x)=> x[Date] <= [Date] and Date.QuarterOfYear(x[Date]) = Date.QuarterOfYear([Date]) and Date.Year(x[Date]) = Date.Year([Date]) ),
    b = List.Sum(a[Amount])
    in b),
        YTD = Table.AddColumn(QTD, "YTD", each let a  = Table.SelectRows(t, (x)=> x[Date] <= [Date] and  Date.Year(x[Date]) = Date.Year([Date]) ),
    b = List.Sum(a[Amount])
    in b)
    in
        YTD