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

Did you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now

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

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

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.