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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
rpratibha86
Frequent Visitor

Create new table: calculate min date of all the dates per record

I have a table with records including multiple dates per record. One person had multiple transactions on multip[le dates. I have to find the min date, first date of transaction and last date of transaction and the amount withdrawn/deposited on those 2 transactions.

I am open to crreating 2 separate tables saying first transaction and last transaction. or having 1 table with both the information.

 

When I am using min date funstion its giving me min of the date column and not per record.

 

Any help is much appreciated

1 ACCEPTED SOLUTION
jahida
Impactful Individual
Impactful Individual

Ok I have no idea what your ideal outcome is... I thought a derived table was the objective, how did it become a workaround?

 

Use the Relationships tab in PowerBI to create the relationship. EmpID isn't a primary key in the initial table because it has duplicates, but it's unique in the second table so should be fine.

View solution in original post

18 REPLIES 18
Eric_Zhang
Microsoft Employee
Microsoft Employee

@rpratibha86

Your requirement is not quite clear for me, based on my understanding, are you looking for something like nested IFs?

 

Capture.PNG

 

minDate = 
IF (
    'table'[date4]
        > IF (
            IF ( 'table'[date1] > 'table'[date2], 'table'[date2], 'table'[date1] )
                > 'table'[date3],
            'table'[date3],
            IF ( 'table'[date1] > 'table'[date2], 'table'[date2], 'table'[date1] )
        ),
    IF (
        IF ( 'table'[date1] > 'table'[date2], 'table'[date2], 'table'[date1] )
            > 'table'[date3],
        'table'[date3],
        IF ( 'table'[date1] > 'table'[date2], 'table'[date2], 'table'[date1] )
    ),
    'table'[date4]
)
maxDate = 
IF (
    'table'[date4]
        < IF (
            IF ( 'table'[date1] < 'table'[date2], 'table'[date2], 'table'[date1] )
                < 'table'[date3],
            'table'[date3],
            IF ( 'table'[date1] < 'table'[date2], 'table'[date2], 'table'[date1] )
        ),
    IF (
        IF ( 'table'[date1] < 'table'[date2], 'table'[date2], 'table'[date1] )
            < 'table'[date3],
        'table'[date3],
        IF ( 'table'[date1] < 'table'[date2], 'table'[date2], 'table'[date1] )
    ),
    'table'[date4]
)

 

 

Alternatively, you can unpivot the date columns and use MAX and MIN.

Hi,

I have an unpivoted date column table only and when i use Max and Min its calculating the min and max of all records and not per record . but the desired output is the same liek your screen shot.

jahida
Impactful Individual
Impactful Individual

Can you add a column to the table with something like this formula?

 

MinDate = CALCULATE(MIN(Table1[Date]), ALL(Table1), Table1[person] = EARLIER(Table1[person]))

MaxDate = CALCULATE(MAX(Table1[Date]), ALL(Table1), Table1[person] = EARLIER(Table1[person]))

getting the following error message:

"A single value for column 'date' in table 'table1' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

jahida
Impactful Individual
Impactful Individual

Can you paste in your exact formula?

 

EDIT: Just reread the error, looks like you're making it as a measure. You need to make it as a new Table, not a new Measure.

As a  workaround I created a derived table only with Emp Id, firsttransaction date , Last Transaction date.

but I want to pull the dates from this table and amount from the parent table but its not letting me pull any column from parent table. How do I create a relation between the two, as EmpId is Primary key for both the tables

That works..All ypour help is much appreciated!

jahida
Impactful Individual
Impactful Individual

Ok I have no idea what your ideal outcome is... I thought a derived table was the objective, how did it become a workaround?

 

Use the Relationships tab in PowerBI to create the relationship. EmpID isn't a primary key in the initial table because it has duplicates, but it's unique in the second table so should be fine.

Actually I was trying to pull all the data fields in one table including min and max dates. But If I do so I am getting duplicates as its pulling all the records.

So the workaround is:Pulling only EmpId, Mindate and Max date in one table. Then Link it to parent table and create reports.

 

But I could not figure out a way to create a link or relationship between the derived table and the parent table.

My apologies for any confusion , I am very new to POwer BI, trying to work it out.

 

MattAllington
Community Champion
Community Champion


@rpratibha86 wrote:

 

 

When I am using min date funstion its giving me min of the date column and not per record.

 


The formula will operate over the filter context.  If you select a single person, it should give max and min for them. If you select all people, it should give min and max for all people. The correct formula depends on what you are trying to achieve. If you can post samples of your desired output and your table structure, I can give you more guidance. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

I am trying to Achineve some thing DaxDesiredPOutput.PNGlike this:

 

jahida
Impactful Individual
Impactful Individual

If you're set on a new table, you can use SUMMARIZE to create it:

 

NewTable = SUMMARIZE(Data, Data[Emp ID], "FirstTransactionDate", MIN(Data[Transactiondate]), "LastTransactionDate", MAX(Data[Transactiondate]))

Tried this solution but its not working..still pulling up all the rows. 

Habib
Responsive Resident
Responsive Resident

instead of creating new tables you should try DAX functions FIRSTDATE and LASTDATE.

I was able to pick min and max dates using firstdate and last date, is there way I can pull the corresponsing transaction amount, like the amount on first date and amount on last date

jahida
Impactful Individual
Impactful Individual

Use the LOOKUPVALUE function. You could use extra clauses in the SUMMARIZE function but it's probably easier to make a new column like:

 

FirstAmount = LOOKUPVALUE(Data[Amount], Data[Transactiondate], NewTable[FirstTransactionDate])

 

Similar for LastAmount.

I am getting the following error:

A table of multiple values was supplied where a single value was expected.

jahida
Impactful Individual
Impactful Individual

Oh wow, I'm not very smart, sorry.

 

FirstAmount = LOOKUPVALUE(Data[Amount], Data[Transactiondate], NewTable[FirstTransactionDate], Data[Emp ID], NewTable[Emp ID])

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.