Find difference between two rows by usind Dax in Power BI - powerbi

I have three column one is Id(ID is same) 2nd col is amount and third is date, I want difference between two rows(amount)

As you want to have the previous value of the date where the ID is equal, you can use the following:
Add a column,
Column4 =
var baseFilter = FILTER(DiffRows;DiffRows[Column1] = EARLIER(DiffRows[Column1]))
var selectDate = CALCULATE(LASTDATE(DiffRows[Column3]);baseFilter;
FILTER(baseFilter; DiffRows[Column3] < EARLIER(DiffRows[Column3])))
return
DiffRows[Column2] - CALCULATE(sum(DiffRows[Column2]);baseFilter;
FILTER(baseFilter; DiffRows[Column3] =selectDate))
First I create a basefilter to ensure the IDs are same.
Next I select the date whcih is the previousdate within the set of same ids
Last I use this date, to filter the correct value out of the rows.
End result:

Related

Power BI DAX How to add column to a calculated table that summarizes another

I Have a TestTable that summarizes a table Receipts on the Month column and adds a column that counts the number of times (occurence) that each month appears in the Receipts Table.
TestTable = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
I want to add two columns to this TestTable which will tell me the following:
Sum the TotalReceiptsIssuedInThisMonth of the TestTable and show the
value in each row
For each Month (row), divide the, TotalReceiptsIssuedInThisMonth by the SumOfTotalReceiptsIssued
I know I can click "New Column" and use these formulas:
AvgPercentageReceiptsIssuedInThisMonth = TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued]
TotalReceiptsIssued = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
However, I need to integrate those two columns directly into the original TestTable formula to make it all happen in one step for use as a variable in the original Receipts table (otherwise I end up with circular logic if I try using relationships).
I've tried the following:
TestTable = SUMMARIZE(PPTs, PPTs[Month], "TotalReceiptsIssuedInThisMonth", SUM(PPTs[PPTs Issued]), "OccurenceOfMonth", COUNT(PPTs[Month]), "TotalReceiptsIssued", SUM(TestTable[TotalReceiptsIssuedInThisMonth]), "AvgPercentageReceiptsIssuedInThisMonth", TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued])
but this returns an error saying "A single value for column 'TotalReceiptsIssuedInThisMonth' in table 'TestTable" 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." and I've tried:
TestTable =
VAR first = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
VAR second = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
VAR third = first[TotalReceiptsIssuedInThisMonth]/second
RETURN
third
But this returns an error saying "The variable'first' cannot be used in current context because a base table is expected."
So my question is, how do I go about combining these three steps into one DAX formula?
I would do something like this. I prefer ADDCOLLUMN(SUMMARIZE()...), because it helps to avoid a miscontexting. As you need a var table, then you need the CALCULATE in ADDCOLUMNS, as it adds the row context.
VAR TestTable =
ADDCOLUMNS(
SUMMARIZE(
Receipts
,Receipts[Month]
)
,"TotalReceiptsIssuedInThisMonth",CALCULATE(SUM(Receipts[Receipts Issued]))
,"OccurenceOfMonth", CALCULATE(COUNT(Receipts[Month]))
,"TotalReceiptsIssued ",SUM(Receipts[Receipts Issued])
)
RETURN
ADDCOLUMNS(
TestTable
,"AvgPercentageReceiptsIssuedInThisMonth"
,DIVIDE([TotalReceiptsIssuedInThisMonth],[TotalReceiptsIssued])
)
Check out More regarding the use of DAX to create a table
https://www.advancelearnlinux.com/how_to_create-table-in-microsoft-power-bi-using-dax-function/

PowerBI: Aggregate Measure correctly by condition on DATEDIFF

I have the following Table:
BaseTable
It represents processes with a certain category.
And there is also a Date Table over column TIMESTAMP.
I would like to show a Measure based on another Measure that calculates the Date-Difference until the selected Date.
So first this is how I calculate the Date-Difference:
AGE =
VAR SELECTED_DATE = CALCULATE(MAX(DATUM[Date]), ALLSELECTED(DATUM))
VAR STARTDATE_PROCESS = Calculate(MAX(Workflow[MIN_TIMESTAMP]),DATUM[Date]<=MAX(DATUM[Date]), ALL(DATUM[Date]))
RETURN
DATEDIFF(STARTDATE_PROCESS,SELECTED_DATE,DAY)
Now I want to use a Measure which depends on the result of AGE, like
NEW = IF([AGE]<=3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
or
OLD = IF([AGE]>3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
The Measures AGE, OLD and NEW look like that with the Base Table:
Measures
As you can see the aggregation is not working correctly:
Result_Wrong
But it should be like that
Result_Correct
Any idea how to fix that?
Thank you!
So the problem is that the subtotal is calculated at a whole different context, and because your Age measure is based on the MAX(Workflow[MIN_TIMESTAMP]) that won't take into account that there can be multiple processes.
To do what you want, you need to change the New and Old measures to perform an aggregation per process and then return the result of that. Something like this:
New_agg =
VAR tbl = ADDCOLUMNS(CALCULATETABLE(VALUES(Workflow[Process]), ALL('Date')), "age", [Age], "count_process", CALCULATE(COUNT(Workflow[Process]), ALL('Date')))
RETURN SUMX(tbl, IF([age]<=3, [count_process]))
Demo File
Let me know if below solution is working
Unfortunately I am unable to generate the dummy data that you have been using, so Created my own data for developing the solution.
Now from this data I have calculated the difference of dates and put it as Age
Now to get the count of process for the condition like yours, I have created two formulas and the result is:
Logic I followed here is, instead of creating measure I have created columns and took the sum of those columns which will give the data you need as sum of those columns.
Column for New:
New = IF((Sheet1[Age]) > 20, 1,0)
Column for Old:
Old = IF((Sheet1[Age]) < 20, 1,0)
Now place both formulas in "Values" and take sum as the aggregation.
Final result is

How to create a new column from the select of the distinct values of two columns with Power Query Editor in Power Bi

I need to insert in a new column the distinct values of two columns using the Power Query Editor Power Bi.
Any ideas enter image description hereguys?
Here you go:
let
Source = SomeTable
firstCol = Source[FirstColumn],
secondCol = Source[SecondColumn],
thirdCol = List.Sort(List.RemoveNulls(List.Distinct(List.Combine( {firstCol, secondCol})))),
#"TableResult" = Table.FromColumns({firstCol, secondCol, thirdCol}, {"First","Second","Combined"})
in
#"TableResult"
This basically converts your first and second columns into lists, and combines them into one new list. Next, it transforms the new list a bit to match your requirements -- first by getting just the distinct values, then dropping any null values, and lastly sorting it in ascending order.
Once that's done, we can take advantage of Table.FromColumns and create a table from our three lists.
That should get you where you're going.
Thanks Ryan.
My requirement was to create a distinct list of IP addresses from one of the columns in a Table.
Here's the code that worked for me.
= let
Source = #"TABLENAME",
firstCol = #"TABLENAME"[Client Ip],
IP = List.Distinct(firstCol),
#"DistinctIP" = Table.FromColumns({IP}, {"IP"})
in
#"DistinctIP"

Power BI Lookup with Duplicates Equation

I have a data set that contains duplicates and i am trying to do the equivalent of a Vlookup from excel. In excel when you use the vlookup function it will just return the first value even if there is a duplicate. The data set that i am working with has a unique 16 character string.
I have utilized some videos, forms, and other resources but no luck. I have used the calculation equation with a first non blank and a filter but i either get an error or returns blank.
https://1drv.ms/x/s!AtrxZbQBYb0LjZtaIkZcn4qsMimwnQ?e=PZbNud
Column = CALCULATE(
FIRSTNONBLANK('Table1'[ID]),
FILTER('Table1','Table1'[Parent]='Table1'[ID]))
You can go with:
Column =
var SampleID = 'Sample Data'[ID]
var EarliestDate = CALCULATE(MIN('Sample Data'[Creation Date]);FILTER('Sample Data';'Sample Data'[Parent] = SampleID))
return CALCULATE(MIN('Sample Data'[Text]);FILTER('Sample Data';'Sample Data'[Parent] = SampleID && 'Sample Data'[Creation Date] = EarliestDate))
Note: it finds the earliest date ann when dates are equal, smallest string
Enjoy!

Need to help build a control column that finds a bug with DAX

Power BI Desktop, DAX
I need to help build a control column that finds a bug.
I have three columns: "SN" - serien nr. Data type: text, "MTH" Type data: Whole Number and "Date" Data type: Date.
Each SN has x Mth. Every Mth has just one date.
For each SN, it is true that it can not have more Mth at an earlier date.
Example:
I solved it only by counting the help tables in Query Editor, which took a lot of performance.
I was able to achieve this using the following calculated column:
Control =
VAR BugSN = Bug[SN]
VAR BugMth = Bug[Mth]
VAR BugDate = Bug[Date]
RETURN CALCULATE(
MAX(Bug[Date]),
ALL(Bug), Bug[SN] = BugSN, Bug[Mth] = BugMth
) = BugDate
What this says is that if the date in that row is the max for that SN and Mth combination, then TRUE otherwise FALSE.
(I named the table Bug, but you'll need to replace that with whatever your table name is.)