Finding the first date between multiple entries // PowerBI - powerbi

My co-worker has run into an issue finding the amount of days between 2 different entries on a database.
In this database , there can be multiple entires with the same RegNumber , however sometimes the entries are on separate dates.
We would need to fetch the first date (TxrDate) for each RegNr and add it to the "FirstInvDate" column on each line.
Please see the below sample data:
And the below is what he has tried:
Does anyone know if there is an easier way to do this or a specific formula to follow ?

To create a new table use:
FistInvDate =
var __currRegNumber = 'Table'[RegNumber]
return
calculate( min( table[yourDate]), filter(ALL('table'), __currRegNumber = 'Table'[RegNumber]))

Related

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

IF condition in DAX is creating Cartesian Join on rows

Dax formula without If condition works fine, but as soon as I add an IF condition rows start multiplying. I believe it's doing Cartesian Product.
My requirement is simple. I need to show only those rows in which Sickness[Start_Date]>LASTDATE(Test[Date])
The complete formula is -
Measure =
var val = CALCULATE(MAX(Sickness[Start_Date]),FILTER(Sickness,Sickness[Start_Date]>LASTDATE(Test[Date])),ALL())
return
IF(val = BLANK(),0,1)
I have 3 separate tables Emp_data, Test and Sickness.
To replicate this scenario, Please follow the below steps:
1st Step : Create table Emp_data
Emp_data = DATATABLE("Emp_no",INTEGER,"Name",STRING,{{101,"A"},
{102,"B"},
{103,"C"},
{104,"D"},
{105,"E"},
{106,"F"},
{107,"G"},
{108,"I"},
{109,"J"},
{110,"K"},
{111,"L"},
{112,"N"},
{113,"M"},
{114,"O"},
{115,"P"},
{116,"Q"},
{117,"R"},
{118,"S"},
{119,"T"},
{120,"U"}
})
create table : Test
Test = DATATABLE("Emp_No",INTEGER,"Date",DATETIME,"Result",INTEGER,{{101,"3/10/2020",1},
{101,"3/13/2020",2},
{102,"3/11/2020",1},
{103,"3/12/2020",2},
{104,"3/13/2020",1},
{105,"3/14/2020",1},
{106,"3/15/2020",2},
{107,"3/16/2020",1},
{108,"4/20/2020",1},
{109,"4/21/2020",2},
{110,"4/22/2020",2},
{111,"4/23/2020",1},
{112,"4/24/2020",1},
{113,"4/25/2020",2},
{114,"4/26/2020",1},
{115,"4/27/2020",2},
{116,"5/5/2020",1},
{117,"5/5/2020",1},
{118,"5/5/2020",1},
{119,"5/5/2020",1},
{120,"5/5/2020",2}
})
Create table Sickness
Sickness = DATATABLE("Emp_no",INTEGER,"Start_Date",DATETIME,"End_Date",DATETIME,"Sickness_Code",INTEGER,{{101,"2/12/2020","2/12/2020",30},
{101,"3/10/2020","3/15/2020",50},
{101,"3/20/2020","3/30/2020",50},
{101,"4/5/2020","4/10/2020",40},
{102,"3/11/2020","3/11/2020",50},
{107,"3/1/2020","3/2/2020",30},
{107,"3/15/2020","3/20/2020",50},
{107,"3/21/2020","3/31/2020",40},
{112,"4/20/2020","4/30/2020",50},
{112,"5/1/2020","5/15/2020",50},
{116,"4/1/2020","4/15/2020",30},
{116,"5/3/2020","5/15/2020",50},
{116,"5/16/2020","5/26/2020",50},
{116,"5/27/2020","5/29/2020",40}}
)
Second Step is to create relationship between these 3 tables. Emp_Data and Test table have one to many relationship. I changed the filter direction to BOTH. Thinking may be this will resolve the issue. Create relation many to many between the tables Test and Sickness. The image is attached
The output is as follows :
If I remove the IF condition my out is what I need :
Can someone help me in understanding this behavior of Power BI. This looks very strange. Why row count is increasing after applying IF condition. Thanks in advance
It's because you assigned a 0 value to all blank values in your Sickness data table. Once your remove the if statement, Power BI only shows data rows that do not return a blank value.
To fix your issue try this updated meassure:
Measure =
VAR val = CALCULATE(MAX(Sickness[Start_Date]),FILTER(Sickness,Sickness[Start_Date]>LASTDATE(Test[Date])),ALL())
VAR OneZero = IF(ISBLANK(val),0,1)
RETURN
IF(HASONEVALUE(Sickness[Emp_no],OneZero)

How to select the last value of the day with DAX in Power BI

I am new to power BI and stuck with an issue. I have my model as follows:
Date Dimension
Measurement Fact
The date column in Date Dimension is link to measuredate in Measurement Fact
Below is a sample data:
NB: In edit query, I have changed the type of measuredate to Date only.
I have tried the measure below but it doesn't work the way I want. It will sum all the values of the day but what I want is the last value of the day:
day_fuel_consumption =
CALCULATE (
SUM ( measurement[measurementvalue] ),
FILTER (
measurement,
measurement[metername] = "C-FUEL"
&& measurement[measuredate] = MAX ( measurement[measuredate] )
)
)
My Goal is to get 29242, i.e the last value of the day. Remember that measuredate is a Date field and not Datetime (I changed to Date field so that my Year and Month filter can work correctly). I have changed the type in edit query.
Changing your measure to use a variable could be the solution:
DFC =
var maxDate = MAX(measurement[measuredate])
return
CALCULATE(
SUM(measurement[measurementvalue]),
measurement[measuredate] = maxDate
)
However, you should keep the datetime format for measureDate. If you don't want to see the time stamp just change the format I power bi. Otherwise power bi will see two values with max date and sum them, instead of taking the last one.
Well, if you want to avoid creating a measure, you could drag the fields you are filtering over to the visual filters pane. Click your visual, and scroll a tiny bit and you will see the section I am referring to. From there, just drag the field you are trying to filter In this case, your value. Then select "Top N". It will allow you to select a top (number) or bottom (number) based on another field. Strange enough, it does allow you to do top value by top value. It doesn't make sense when you say it out loud, but it works all the same.
This will show you the top values for whatever value field you are trying to use. As an added bonus, you can show how little or how many you want, on the fly.
As far as DAX goes, I'm afraid I am a little DAX illiterate compared to some other folks that may be able to help you.
I had to create two separate measures as shown below for this to work as I wanted:
max_measurement_id_cf = CALCULATE(MAX(measurement[measurementid]), FILTER(measurement, measurement[metername] = "C-FUEL"))
DFC =
var max_id_cf = [max_measurement_id_cf]
return
CALCULATE(SUM(measurement[measurementvalue]), measurement[measurementid] = max_id_cf)

How to calculate conflicting meeting using data from Exchange server in PowerBI

I am trying to get data insights of my calendar through visualizations in PowerBI. I am able to get almost all data from my outlook calendar using in-house API in PowerBI. I intend to find how many conflicting meetings I have per week, but I couldn't find any flag column for that. I'm trying to use time slicers to generate a what-if parameter to calculate a flag, but it doesn't work. Is there any way I can track conflicting meetings?
The data I have relative to meetings is as below -
You could add a Calculated Column to the dataset, with a formula like this:
Conflicting =
VAR StartDate = 'Calendar'[Start]
VAR EndDate = 'Calendar'[End]
VAR IDCurrent= 'Calendar'[Id]
RETURN
IF (
COUNTROWS(
FILTER (
ALL('Calendar');
'Calendar'[Start] < EndDate &&
'Calendar'[End] > StartDate &&
'Calendar'[Id] <> IDCurrent
)
) > 0; TRUE(); FALSE())
This formula checkes if there are different rows within the same date range.
You can adjust the date comparions based on your needs. I've got the logic from this post and removed the equal signs, to prevent contiguous items marked as overlapping.
The Id column is the Unique Identifier (like a unique, primairy key) automaticly provided by Exchange Online. The filter on Id <> IDCurrent makes sure you're not mark the current row as overlapping, e.g. it searches for all rows exept the current one.:
Result:
Edit: The formula above results in a true/false value. You can easily remove the if statement, to count the conflicting appointements, but remember that the value will be counted twice (or more); for each conflicting appointment.

DAX: How do i create a table for a burndown chart? What status was current on certain dates

I'm creating a dashboard for a sprint overview and want to visualize the progress of the team in a burndown chart. The chart should give information about the amount of user stories that are each new, active and closed. So that in the beginning all user stories are open, then they become active and in the end, there are no open and active stories left.
Now my problem is in modeling the data using DAX.
The data is stored in a big table with a row for each user story that contains all the information on that date. Now, if the information gets modified like in the event of changing the status from new to active or correcting a spelling mistake, the program just adds a new row with a new date.
like in here
The table I want should have the columns date, new, active, and closed. For the date column i have written the following code:
CALENDAR(
FIRSTNONBLANK(
CurrentIterations[StartDate];
CurrentIterations[StartDate] );
FIRSTNONBLANK(
CurrentIterations[FinishDate];
CurrentIterations[FinishDate])
)
But now, oriented on that dates i want the other columns to calculate themselves. In every row I want the amount of user stories in the original table that are active and the latest version on that date.
Examples:
Original table
Wanted table
Wanted burndown chart
Any help greatly appreciated!
Here's another approach.
Calculated table:
MaxRev = CROSSJOIN(VALUES(Test[Id]), VALUES(Test[Date]))
Add the following calculated columns to this table:
MaxRev = CALCULATE(MAX(Test[Rev]),
FILTER(Test, Test[Id] = MaxRev[Id] && Test[Date] <= MaxRev[Date]))
Status = LOOKUPVALUE(Test[State], Test[Id], MaxRev[Id], Test[Rev], MaxRev[MaxRev])
Then use this to create a new calculated table:
Wanted = SUMMARIZE(MaxRev, MaxRev[Date],
"New", CALCULATE(COUNT(MaxRev[Id]), MaxRev[Status] = "New") + 0,
"Active", CALCULATE(COUNT(MaxRev[Id]), MaxRev[Status] = "Active") + 0,
"Solved", CALCULATE(COUNT(MaxRev[Id]), MaxRev[Status] = "Solved") + 0)
Well, it isn't as beatiful, but it does the job. I've created an extra table, to get the last Rev per Id and Day. At least, that's what I thought you meant.
I'm open for better solutions, I'm convinced it can be done better/easier.
'Test' is your original table, make a link on Date to your already created table (Wanted) with the Date column.
Calculated table:
MaxRev =
SUMMARIZE(Test; Tabel[Date]; Test[Id]; "Max"; MAX(Test[Rev]))
Column New (add to table with single date column):
New =
CALCULATE (
DISTINCTCOUNT ( Test[Id] );
CALCULATETABLE (
FILTER (
CROSSJOIN ( 'MaxRev'; Test );
'MaxRev'[Id] = Test[Id]
&& 'MaxRev'[Date] = Test[Date]
&& 'MaxRev'[Max] = Test[Rev]
)
);
Test[State] = "New"
) + 0
Repeat this column, also for Active and Solved.
PBIX file