PowerBI I need to calculate the datediff - powerbi-desktop

in PowerBI I need to calculate the datediff between the date on the row to the maximum date possible on the table
Example
Table A ( this table has no id for each row)
Row . Date
1 . 1-jan-2000
2. 7-jan-2000
3. 8-jan-2000
4. 10-jan-2000
5. 31-jan-2000
What I am trying to get
Row . Date . Maximum Date in Table Calculated Column DateDiff (days)
1 . 1-jan-2000 . 31-jan-2000 30
2. 7-jan-2000 . 31-jan-2000 24
3. 8-jan-2000 . 31-jan-2000 23
4. 10-jan-2000 . 31-jan-2000 21
5. 31-jan-2000 . 31-jan-2000 0
I have tried to create a measure of the max date, and used last date, but when looking at the row it just gives the date as the current date of the row and not its max date of the table
every row should have the same maximum date, and i can then calculate the diff between the date on the row and the max date

I started with data given below -
Step 1:-
Make Sure your Date Column in the Table belongs to data type "Date".
Step 2:-
Create a calculated column using DAX then, with the DateDiff function
Date Diff = DATEDIFF(MyTable[Date],MAX(MyTable[Date]),DAY)
Here is a link to learn the DATEDIFF function.
The output looks like below :-
Kindly accept the answer if it works for you or let me know if it doesn't.

Related

How to make a pivot table that can filter week and month from dates in horizontal?

I am so confused with the pivot table options. I have used it before but I am not getting the desired results.
My table looks like this
What I need is to make a pivot table that will show names in the first column and then show the sum of numbers grouped by month in columns.
For example:
Name 7 8 <-----Months
Oliva M 30 30 <-----Sum of the 1s in the row
Eboni Sh 30 30
Stefanie 25 12
Thanks for your help

How to sum up to reach a number by date in excel

I would like to make a formula that to do a sum calculation, that confirm which date to start (cell D5), then look for the start point from the row 1, and sum up the row 2 start from the correct date, and then stop the sum up if the amount is equal to or larger than run estimate (cell D6)
the steps should be like this:
I know the start day is 31-May
then I look into the table to find the date 31-May
then I look into the table again, find out the run estimate is 2 hours
I know I have 6 hours run estimate need to be completed
then I add 2 + 3 + 4 = 9 hours to cover the 6-hour estimate
then I know the end date is 2- Jun
I am able to find the start point on the table
=SUMIFS(B2:F2,B1:F1,">="&D5)
but I am not sure how to stop it
and moreover I am not sure how display the final value date, not the total amount..
col B col C col D col E col F
30-May 31-May 1-Jun 2-Jun 3-Jun
1 2 3 4 5
start date 31-May
run estimate 6
expected result should be 2-Jun
enter image description here
anyone can help on this?
Here is a solution that requires one extra row of data, to calculate the sum of run times up until that day: Put this formula in cells B3:F3 (or however many columns you have exactly)
=SUMIF($B1:B1,">="&$D6,$B2:B2)
Then you can find the first value that is equal to or higher than your estimate runtime and use that to offset to the correct date:
=OFFSET(B1,0,IFERROR(MATCH($D6,B3:F3,0)-1,MATCH($D6,B3:F3,1)))

Excel Formula - get the date of a maximal value within a list

I have a rather big (long) table and need to do something quite simple but I'm currently with a sort of blackout...
Imagine that you have:
Date 1 Value 1 Date 2 Value 2 Date 3 Value 3 Date of MAX
... ... ... ... ... ... ????
I want to deploy in ???? a formula that will result in the date at which the maximal value (between Value 1...3) was registered. For instance:
Date 1 Value 1 Date 2 Value 2 Date 3 Value 3 Date of MAX
20160501 10 20160722 47 20161002 9 20160722
meaning, the Date of MAX is of Date 2 because that was the date at which the MAX was measured.
Thanks in advance.
You could do a more general solution using offset:-
=MAX(N(OFFSET(D2,0,COLUMN(A:D)*3)))
to find the largest value - put this in (say) R2.
Then find a match for it and pick out the corresponding date:-
=OFFSET(C2,0,MATCH(R2,N(OFFSET(D2,0,COLUMN(A:D)*3)),0)*3)
assuming the dates and values are every third column.
These are array formulae and must be entered with CtrlShiftEnter
If the last value really was in P21 you would have to give a row offset as well as a column offset.
OK, I found a dirty but simple solution (don't know why I didn't think of it at first):
=IF(G2>=MAX(G2,J2,M2,P21),F2,IF(J2>=MAX(G2,J2,M2,P21),I2,IF(M2>=MAX(G2,J2,M2,P21),L2,O2)))
where the pairs (4 of them) are at FG, IJ, LM, OP. Not elegant but does the job.

DAX Calculation with Datesbetween Ignores all Datefilters

I am starting with DAX.
I have a standard date table and a table with sales. When I try to show the running total with the below formula the month filter is ignored, as shown:
The formula is:
Prov YTD VJ 2:=CALCULATE(FIRMPROV[SumProv];
All(Kalender[Jahr]);
DATESBETWEEN(Kalender[Datum];
Min(START_YTD[start_ytd]);
MIN(END_YTD[end_ytd])))
A good way to calculate running totals is by using the FILTER and EARLIER functions, but a prerequisite to that is that you need to create a Month Number column (ie. Jan = 1, Feb = 2, Mar = 3, etc.)
Then, you can use the following DAX:
CALCULATE(SUM([SumProv]),ALL(FirmProv),FILTER(FirmProv,[Month Number]<=EARLIER([Month Number])))
EARLIER basically is DAX's way of interpreting the current row. You're telling DAX to sum ALL of the rows in the FirmProv table where the month number is less than or equal to the current row (AKA running total).
Here's the result (note the numbers aren't exactly like yours, I just quickly mocked it up to show the example):

Compare values previous date and second previous date in PowerPivot

I'm new to PowerPivot and DAX. I've followed some on-line tutorials. Now I have a small problem that I can't solve. I have the following data:
Date Instrument Value
2016-07-27 A 100
2016-07-27 B 98
2016-07-26 A 102
2016-07-25 B 99
For each date I would like to calculate the difference (Profit/Loss) in Value between most recent date and second most recent date. For the data above it would be the following:
Date Instrument Value Profit/Loss
2016-07-27 A 102 2 ([Val. inst. A 2016-07-27]-[Val. inst. A 2016-07-26])
2016-07-27 B 98 -1 ([Val. inst. B 2016-07-27]-[Val. inst. B 2016-07-25])
2016-07-26 A 100
2016-07-25 B 99
I have tried with DAX to find the second largest date using =EARLIER([Date])but haven't managed to get it to work. With the second largest date I would maybe be able to find the Value corresponding to that date. Any suggestions how this could be solved?
In the end I came up with a solution in three steps (the steps can be combined into one step). First I rank all the dates, with the most recent being 1 and the second most recent being 2. After that I retrieve the Value for the second most recent day for each row. Finally I calculate the difference between the Values current row and the Value for the second most recent day compared to that row's date.
To rank the dates I used the following:
=RANKX(FILTER(ALL(table);EARLIER([Instrument])=[Instrument]);[Date];;FALSE())
Explanation to what I think the DAX formula is doing. RANKX works by taking a table and then rank the values in a column in that table. Above I've used a filtered table as the table. The filtered table creates a new table for each row containing only the same instrument as the instrument for that particular row. For the first row, the filtered table would look like below.
Date Instrument Value
2016-07-27 A 100
2016-07-26 A 102
The dates in that filtered table is then ranked.
Date Instrument Value Rank
2016-07-27 A 100 1
2016-07-26 A 102 2
Using the Rank I then pull out the second most recent dates Value for each row based on the current row's Rank-1.
Value second most recent date = CALCULATE(MAX([Value]);FILTER(table;EARLIER([Instrument])=[Instrument] && [Date Rank]= EARLIER([Date Rank]))
Finally I calculate the difference:
PnL = [Value] - [Value second most recent date]
I'm not sure what EARLIER is doing but I think it is some sort of iterative process.

Resources