Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

This week in Dax

Hi,

 

I am needing to to do a caulate measure to find out this weeks amount and last weeks amount.

 

Would it be something like

 

if(calender.day = Tuesday(calculate(calculate,sum(value),calenderdate-1),calenderdate-1)

 

What would be the best way to acheive this?

 

Thanks

 

Chris

  • I created the following two enter data queries:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc7BCQAhDEXBXjwvxPysxq1F7L8NQVjwHec2c5Y076bqWdZzlNCAvluqkEOCAnqhBuEiXISLcAlc4roMA/7X2g==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
    in
        #"Changed Type"

    That one is called Calendar.

     

    The next one is called Values:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Rc/BDYAgEAXRXjibwF/QhVoI/behUWe5vtPMnMmzrmxFno6ktI5XHDGkIxUZSPvFCnIiQi7EEEcq0pGGDOSMwgLtaKotqkW2RbbotugW4TXCRXmNcn3pPW/xX+JFT/q6AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Value", Int64.Type}})
    in
        #"Changed Type"

    Related the tables on "Date" column.

     

    Created the following calculated colunm in both tables:

     

    Weeknum = WEEKNUM([Date])

    Created the following Measure:

    MyTotal = VAR myMax = MAX('Calendar'[Weeknum]) RETURN CALCULATE(SUM([Value]),ALLEXCEPT('Values','Values'[Weeknum]),'Values'[Weeknum]=myMax)

     

     

  • To make it honor the slicer selection, add that slicer column to the ALLEXCEPT clause.

11 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Greg_Deckler

       

      It doesnt seem to allow weeknum in the calculate function.

       

      weekly pageviews = CALCULATE(SUM(GA[Pageviews],WEEKNUM()

       

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Was missing a bracket!!

         

        Works a treat :)

    • Anonymous's avatar
      Anonymous
      Not applicable

      Nope still does not work

       

      weekly pageviews = CALCULATE(SUM(GA[Pageviews]),WEEKNUM('Calendar$'[Date],2))

       

      Seems to retrun a static number... when i select another month or different week the figure does not change...any ideas?

       

      Im thinking something like the below ...but min does not work

       

      weekly pageviews = CALCULATE(SUM(GA[Pageviews]),DATESBETWEEN('Calendar$'[Date],min(WEEKNUM('Calendar$'[Date],1)),'Calendar$'[Date]))
      • danextian's avatar
        danextian
        Icon for Super User rankSuper User

        Do you have a sample data to work with? At least a simple table.