Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

dax for calculating sum PTD based on different columns

hello Power BI users,

 

I have a table with columns like below and I would like help to create a new calculate column/measure (PTD). This PTD is based on Site,Month and Week selected. 

 

SiteMonthWeekSalesPTD
AJAN159
AJAN149
AJAN2318
AJAN2618
AJAN3422
AJAN4527
BJAN159
BJAN149
BJAN2318
BJAN2618
BJAN3422
BJAN4527
  • It cannot be a calculated column. Those never recalculate during use and ignore user selection.

     

    A measure of = SUM(Table[Sales]) will work. If you put the site, Month, and Week in a visual and filter based on those, it will automatically calculate your values. Nothing fancy needed at all.

     

    If you are trying to get a cumulative total, the correct way is to use a date table, but you can do it with your data.

    PTD = 
    VAR varCurrentSite = MAX('Table'[Site])
    VAR varCurrentMonth = MAX('Table'[Month])
    VAR varCurrentWeek = MAX('Table'[Week])
    RETURN
    CALCULATE(
        SUM('Table'[Sales]),
        FILTER(
            ALL('Table'),
            'Table'[Site] = varCurrentSite
                && 'Table'[Month] = varCurrentMonth
                && 'Table'[Week] <= varCurrentWeek
        )
    )
  • Hi, Anonymous , you might want to try such a calculated column

    PTD = 
    SUMX (
        FILTER (
            Table1,
            Table1[Site] = EARLIER ( Table1[Site] )
                && Table1[Month] = EARLIER ( Table1[Month] )
                && Table1[Week] <= EARLIER ( Table1[Week] )
        ),
        Table1[Sales]
    )

3 Replies

  • edhans's avatar
    edhans
    Icon for Community Champion rankCommunity Champion

    It cannot be a calculated column. Those never recalculate during use and ignore user selection.

     

    A measure of = SUM(Table[Sales]) will work. If you put the site, Month, and Week in a visual and filter based on those, it will automatically calculate your values. Nothing fancy needed at all.

     

    If you are trying to get a cumulative total, the correct way is to use a date table, but you can do it with your data.

    PTD = 
    VAR varCurrentSite = MAX('Table'[Site])
    VAR varCurrentMonth = MAX('Table'[Month])
    VAR varCurrentWeek = MAX('Table'[Week])
    RETURN
    CALCULATE(
        SUM('Table'[Sales]),
        FILTER(
            ALL('Table'),
            'Table'[Site] = varCurrentSite
                && 'Table'[Month] = varCurrentMonth
                && 'Table'[Week] <= varCurrentWeek
        )
    )
  • CNENFRNL's avatar
    CNENFRNL
    Icon for Community Champion rankCommunity Champion

    Hi, Anonymous , you might want to try such a calculated column

    PTD = 
    SUMX (
        FILTER (
            Table1,
            Table1[Site] = EARLIER ( Table1[Site] )
                && Table1[Month] = EARLIER ( Table1[Month] )
                && Table1[Week] <= EARLIER ( Table1[Week] )
        ),
        Table1[Sales]
    )

  • Icey's avatar
    Icey
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    Please let us know if the replies above are helpful.

     

    If they are, please always accept the replies making sense as solution to your question so that people who may have the same question can get the solution directly.

     

    If not, please give us more details.

     

     

    Best Regards,

    Icey