Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Coriel-11
Resolver II
Resolver II

Total for last 30 days when no entries for particular dates

I have a table which is essentially like the one below.  Each time one gives us a "Sentiment" of Positive/Neutral/Negative/Unclassified that sentiment is added as a new line, along with the date.

I have dates that go back to the start of the year so I want to record how many of each category have been recorded over the previous 30 days (including the day in question), so I can produce 100% stacked chart like this:

Coriel11_0-1683301924826.png

So I created this measure:

 

Sentiment MMT = 
    CALCULATE( 
        Count(Sentiment[Sentiment]),
        DATESINPERIOD(
          'Sentiment'[Date],
            MAX('Sentiment'[Date]),
           -1,MONTH
       )
    )

 

The problem I'm having is that if on one of the days it happens that no-one recorded a particular sentiment, then rather than counting up all entries for the other 29 days, it just returns a blank – you can see wome exmaples in the bottom right where there are no grey values.

An example of the data (which doesn't correspond precisely to the chart , it's just to give you an idea)  is below.

 

Does anyone have any ideas?

 

DateSentiment
30-Apr-23Neutral
30-Apr-23Positive
30-Apr-23Unclassified
30-Apr-23Positive
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Positive
29-Apr-23Positive
29-Apr-23Positive
29-Apr-23Positive
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Neutral
29-Apr-23Positive
29-Apr-23Positive

etc.

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@Coriel-11 

Create a DATES table, Link the dates table to your fast table using a one-to-many relation and modify your calculation. Make sure you use the Date column from your Dates table in the Visual. Create a Copy of the Dates table for date selection as well.

 

 


Here is the modified formula:

Sentiment MMT = 
VAR __DateSelected = SELECTEDVALUE('Date Selector'[Date])
VAR __CurrentDate = SELECTEDVALUE('Dates'[Date])
VAR __Period = 
    DATESINPERIOD(
          'Dates'[Date],
            __CurrentDate,
           1,
           MONTH
       )
Return

IF(
    __DateSelected IN __Period,
    CALCULATE( 
        Count(Sentiment[Sentiment]),
        __Period 
    )
)

Fowmy_0-1683322883444.png

 

 File is attached

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

2 REPLIES 2
Fowmy
Super User
Super User

@Coriel-11 

Create a DATES table, Link the dates table to your fast table using a one-to-many relation and modify your calculation. Make sure you use the Date column from your Dates table in the Visual. Create a Copy of the Dates table for date selection as well.

 

 


Here is the modified formula:

Sentiment MMT = 
VAR __DateSelected = SELECTEDVALUE('Date Selector'[Date])
VAR __CurrentDate = SELECTEDVALUE('Dates'[Date])
VAR __Period = 
    DATESINPERIOD(
          'Dates'[Date],
            __CurrentDate,
           1,
           MONTH
       )
Return

IF(
    __DateSelected IN __Period,
    CALCULATE( 
        Count(Sentiment[Sentiment]),
        __Period 
    )
)

Fowmy_0-1683322883444.png

 

 File is attached

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thank you!!. I had tried that, but for some reason I hadn't added the date table date to the visual's X axis, so I'd disregarded that approach and gone back to trying to do something more Dax-y.

Much appreciated. It was driving me mad!

Matt

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.