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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
francesca289
New Member

Show Prior Week Values together with Current Week Values

Hey community! 

 

I have been going crazy about this issue for the whole day and would greatly appreciate your help. I am working with containers, each of which has loading date and an unloading date, see example below:

 

francesca289_0-1699050447761.png

 

I want to see, week by week, the number of containers unloaded against the number of containers loaded during the immediately prior week, see concept explained below in Excel:

 

francesca289_1-1699051318208.png

 

So that I can build a bar chart that looks like this, basing my x axis on the Week Unloaded:

francesca289_2-1699051583544.png

I tried multiple approaches, I created a Prior Week measure and then calculated the number of containers loaded based on it, and I also tried CALCULATE ( DISTINCTCOUNT (table[Container ID], table[Week Loaded]= table[Week Unloaded]-1). I also added an ALL(table(Week Loaded) in the DAX expression above but I can't seem to get the same number of containers loaded that I have for each week, the value is usually lower than what it should be, or it stays the same for all weeks.

 

Your help is REALLY greatly appreciated! 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your datamodel looks like, but I think it depends on how your calendar table looks like and how the relationship between the fact table and the calendar table looks like.

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file. I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_0-1699070912082.png

 

Jihwan_Kim_1-1699071529812.png

Unloaded container this week: = 
CALCULATE (
    COUNTROWS ( DISTINCT ( Data[Container ID] ) ),
    USERELATIONSHIP ( 'Calendar'[Date], Data[Unloaded On] )
)

 

Loaded container previous week: = 
VAR _currentyear =
    MAX ( 'Calendar'[Year] )
VAR _currentweek =
    MAX ( 'Calendar'[Week Number] )
VAR _yearcondition =
    IF ( _currentweek <> 1, _currentyear, _currentyear - 1 )
VAR _previousweek =
    IF (
        _currentweek <> 1,
        _currentweek - 1,
        MAXX (
            FILTER ( ALL ( 'Calendar' ), 'Calendar'[Year] = _currentyear - 1 ),
            'Calendar'[Week Number]
        )
    )
RETURN
    CALCULATE (
        COUNTROWS ( DISTINCT ( Data[Container ID] ) ),
        FILTER (
            ALL ( 'Calendar' ),
            'Calendar'[Year] = _yearcondition
                && 'Calendar'[Week Number] = _previousweek
        )
    )

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
francesca289
New Member

Thank you so much Jihwan! It seems to be working great 👏

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your datamodel looks like, but I think it depends on how your calendar table looks like and how the relationship between the fact table and the calendar table looks like.

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file. I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_0-1699070912082.png

 

Jihwan_Kim_1-1699071529812.png

Unloaded container this week: = 
CALCULATE (
    COUNTROWS ( DISTINCT ( Data[Container ID] ) ),
    USERELATIONSHIP ( 'Calendar'[Date], Data[Unloaded On] )
)

 

Loaded container previous week: = 
VAR _currentyear =
    MAX ( 'Calendar'[Year] )
VAR _currentweek =
    MAX ( 'Calendar'[Week Number] )
VAR _yearcondition =
    IF ( _currentweek <> 1, _currentyear, _currentyear - 1 )
VAR _previousweek =
    IF (
        _currentweek <> 1,
        _currentweek - 1,
        MAXX (
            FILTER ( ALL ( 'Calendar' ), 'Calendar'[Year] = _currentyear - 1 ),
            'Calendar'[Week Number]
        )
    )
RETURN
    CALCULATE (
        COUNTROWS ( DISTINCT ( Data[Container ID] ) ),
        FILTER (
            ALL ( 'Calendar' ),
            'Calendar'[Year] = _yearcondition
                && 'Calendar'[Week Number] = _previousweek
        )
    )

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors