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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Employee
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.

 


@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 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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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