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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Gryphon269
Frequent Visitor

Last 3 Historical values and dates

Hello,

 

I have a table that shows the activity that our animals go through. It shows things like vaccinations and weight recorded during that activity.

 

I am trying to get this table into a matrix that shows the last weight, second to last weight, and third to last weight along with their respective dates. There are more animals as well as other columns, and since the animal is not weighed every time in creates blanks in the table.

 

Current table:

ActivityDateAnimalIDWeight
5/16/20186704 
9/26/20186704460
10/10/20186704420
3/12/20196704650
3/13/20196704 
7/8/20196704 
7/9/20196704750
7/10/20196704 
9/16/20196704 
5/29/20206704810
10/23/20206704940
5/19/20216704 
9/29/20216704920
4/27/20226704830
9/9/202267041000
12/28/20226704 
4/21/20236704806
9/14/20236704 
10/5/20236704834
11/15/20236704 
4/29/20246704850
9/11/20246704984

 

What i want the matrix to look like when 9/11 is selected(or is maxdate):

 

ActivityDateAnimalIDLastWeight DateLastWeightLastWeight-1 Date LastWeight-1LastWeight-2 Date LastWeight-2
9/11/202467049/11/20249844/29/202485010/5/2023834

 

I can the the "LastWeight" well enough using lastnonblank(weight,distinctcount(animalID)), but I am have issues getting the date and other values into measures. 

 

1 ACCEPTED SOLUTION
ThxAlot
Super User
Super User

Last N Date = 
SELECTCOLUMNS(
    INDEX(
        MAX( SLC[Value] ),
        CALCULATETABLE(
            SUMMARIZE( DATA, DATA[ActivityDate] ),
            NOT ISBLANK( DATA[Weight] )
        ),
        ORDERBY( DATA[ActivityDate], DESC )
    ),
    DATA[ActivityDate]
)
Last N Weight = 
SELECTCOLUMNS(
    INDEX(
        MAX( SLC[Value] ),
        CALCULATETABLE(
            SUMMARIZE( DATA, DATA[ActivityDate], DATA[Weight] ),
            NOT ISBLANK( DATA[Weight] )
        ),
        ORDERBY( DATA[ActivityDate], DESC )
    ),
    DATA[Weight]
)

ThxAlot_0-1726602998058.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1726618355538.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
ThxAlot
Super User
Super User

Last N Date = 
SELECTCOLUMNS(
    INDEX(
        MAX( SLC[Value] ),
        CALCULATETABLE(
            SUMMARIZE( DATA, DATA[ActivityDate] ),
            NOT ISBLANK( DATA[Weight] )
        ),
        ORDERBY( DATA[ActivityDate], DESC )
    ),
    DATA[ActivityDate]
)
Last N Weight = 
SELECTCOLUMNS(
    INDEX(
        MAX( SLC[Value] ),
        CALCULATETABLE(
            SUMMARIZE( DATA, DATA[ActivityDate], DATA[Weight] ),
            NOT ISBLANK( DATA[Weight] )
        ),
        ORDERBY( DATA[ActivityDate], DESC )
    ),
    DATA[Weight]
)

ThxAlot_0-1726602998058.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



parry2k
Super User
Super User

@Gryphon269 here is the example output:

 

parry2k_0-1726602401595.png

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Gryphon269 you can do something like this:

 

Last Weight = 
CALCULATE ( 
    MAX ( 'Animal'[Weight] ),
    INDEX ( 
        -1,
        FILTER ( ALL ( 'Animal'[ActivityDate], Animal[AnimalID], Animal[Weight] ), NOT ISBLANK ( Animal[Weight] ) ),
        ORDERBY ( Animal[ActivityDate] ),
        PARTITIONBY ( Animal[AnimalID] )
    )
)

Last Weight - 1 = 
CALCULATE ( 
    MAX ( 'Animal'[Weight] ),
    INDEX ( 
        -2,
        FILTER ( ALL ( 'Animal'[ActivityDate], Animal[AnimalID], Animal[Weight] ), NOT ISBLANK ( Animal[Weight] ) ),
        ORDERBY ( Animal[ActivityDate] ),
        PARTITIONBY ( Animal[AnimalID] )
    )
)
Last Weight - 2 = 
CALCULATE ( 
    MAX ( 'Animal'[Weight] ),
    INDEX ( 
        -3,
        FILTER ( ALL ( 'Animal'[ActivityDate], Animal[AnimalID], Animal[Weight] ), NOT ISBLANK ( Animal[Weight] ) ),
        ORDERBY ( Animal[ActivityDate] ),
        PARTITIONBY ( Animal[AnimalID] )
    )
)

Date Last Weight = 
CALCULATE ( 
    MAX ( 'Animal'[ActivityDate] ),
    INDEX ( 
        -1,
        FILTER ( ALL ( 'Animal'[ActivityDate], Animal[AnimalID], Animal[Weight] ), NOT ISBLANK ( Animal[Weight] ) ),
        ORDERBY ( Animal[ActivityDate] ),
        PARTITIONBY ( Animal[AnimalID] )
    )
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors