I am trying to calculate 'TimeSheet Team PV'[Time (h)] * 'Position History'[Salary]
Data model:
TimeSheet Team PV" table:
"Position History" table:
In the Position History table, I have some employees which were promoted and appear as having different periods during different periods in the company, with different salary. Can I join somehow these values based on Start Date, End Date (Position History table) and Work Date (TimeSheet Team PV tables) + Name?
I would like to generate new column in the TimeSheet Team PV table, with the calculation above.
You'll need to write some logic to make sure it uses the Work Date column. Something like this:
Cost =
VAR WorkDate = 'TimeSheet Team PV'[Work Date]
RETURN
'TimeSheet Team PV'[Time (h)]
* CALCULATE (
SELECTEDVALUE ( 'Position History'[Salary] ),
'Position History'[Start Date] <= WorkDate,
'Position History'[End Date] >= WorkDate
)
Related
I have table Company. I need to display the TOP 6 recently created.
Exemple of data:
Expected results
What I tried :
Rank =
VAR d = Companies[CreatedDate].[Date]
RETURN
CALCULATE (
RANK.EQ ( d, Companies[CreatedDate], DESC)
)
the calculated column returned wrong values:
I need to order by creatd date, and when it's the same date, I need to order by Company Namr
How to correct it!,
For ranking based on created date and company name try the following steps, and if it helps accept it is as answer.
Create a calculated column for ranking the company name.
Company Sort = RANKX(ALL('Table'), 'Table'[Company Name], , ASC)
Create another calculated column for ranking using following DAX
Ranking on Date and Company Name =
VAR X = MAX('Table'[Company Sort])
var res =
RANKX(
ALL('Table),
'Table'[Created Date] * X + 'Table'[Company Sort]
)
RETURN res
Sort by using the column `Ranking on Date and Company Name'.
If you want to create a visual, then just add the column `Ranking on Date and Company Name' in the filter pane and select the Top N filter accordingly.
My challenge has been how to
01 - set Initial Inventory (considering the first day of the column);
02 - From the second day on, have Forecast (Previsão) - Sales (Vendas) = Balance. So 3 columns for each day
The way it is now, it repeats Initial Inventory throughout the days.
Here's the goal I'm trying to achieve:
Here's how the tables are related:
I would try to approach it with an unrelated helper table, that would contain 2 columns and 4 rows. Here's an example based on Contoso data.
Here's the helper table. Column Item ID is used for sorting result columns as well:
And now comes the magic:
Helper measure =
VAR minDate =
CALCULATE ( MIN ( DimDate[Datekey] ), ALLSELECTED ( DimDate ) )
VAR curDate =
SELECTEDVALUE ( DimDate[Datekey] )
RETURN
SWITCH (
SELECTEDVALUE ( Helper[Item ID] ),
1, IF ( curDate = minDate, [Opening Inventory Qty], BLANK () ),
2, [Production Qty],
3, - [Sales Qty],
4, [Closing Inventory Qty]
)
Here's an explanation:
minDate is used to get initial date on the visual
curDate is used to get current date
Next I check, which column from the helper table the measure is calculated for using SWITCH function
using SWITCH, I return different formula, based on helper column type
for Opening inventory, I only return a value when current date is equal to initial date. Otherwise, I return BLANK() - that's why Opening inventory only appears on the first date
The matrix visual is built using Product name in rows; Year, month, day and Item (from the helper table) in columns, and the Helper measure from above.
Here's a the result (don't mind quantities, it's a sample data):
EDIT:
For completeness' sake, here's how the model looks. Notice that the Helper table is not related to any other table:
My intention is to populate days of the month to simulate a data warehouse periodic snapshot table using DAX measures. My goal is to show non-additive values for the quantity.
Consider the following transactions:
The granularity of my snapshot table is day. So it should show the following:
Take note that a day may have multiple entries but I am only interested in the latest entry for the day. If I am looking at the figures using a week period it should show the latest entry for the week. It all depends on the context fixter.
However after applying the measure I end up with:
There are three transactions. Two on day 2 and the other on day 4. Instead of calculating a running total I want to show the latest Qty for the days which have no transactions without running accumulating totals. So, day 4 should show 4 instead of summing up day 3 and day 4 which gives me 10. I've been experimenting with LASTNONBLANK without much success.
This is the measure I'm using:
Snapshot =
CALCULATE(
SUM('Inventory'[Quantity]),
FILTER(
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX( 'Date'[Date] )
)
)
There are two tables involved:
Table # 1: Inventory table containing the transactions. It includes the product id, the date/time the transaction was recorded and the quantity.
Table # 2: A date table 'Date' which has been marked as a date table in Power BI. There is a relationship between the Inventory and the Date table based on a date key. So, in the measure, 'Date'[Date] refers to the Date column in the Date table.
You can use the LASTNONBLANKVALUE function, that returns the last value of the expression specified as second parameter sorted by the column specified as first parameter.
Since LASTNONBLANKVALUE implicitly wraps the second parameter into a CALCULATE, a context transition happens and therefore the row context is transformed into the corresponding filter context. So we also need to use VALUES to apply the filter context to the T[Qty] column. The returned table is a single row column and DAX can automatically convert a single column, single row table to a scalar value.
Then, since we don't have a dimension table we have to get rid of cross-filtering, therefore we must use REMOVEFILTERS over the whole table.
the filter expression T[Day] < MaxDay is needed because LASTNONBLANKVALUE must be called in a filter context containing all the rows preceding and including the current one.
So, assuming that the table name is T with fields Day and Qty like in your sample data, this code should work
Edit: changed in order to support multiple rows with same day, assuming the desired result is the sum of the last day quantities
Measure =
VAR MaxDay =
MAX ( T[Day] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
T[Day],
SUM ( T[Qty] )
),
T[Day] <= MaxDay,
REMOVEFILTERS ( T )
) + 0
Edit: after reading the comments, this might work on your model (untested)
Measure =
VAR MaxDay =
MAX ( 'Date'[Date] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
Inventory[RecordedDate],
SUM ( Inventory[Quantity] )
),
'Date'[Date] <= MaxDay
) + 0
In this measure - I'm creating a temp table to group by customer ID and return the year of the min order_date for each customer. I then want to count the # of customers that show up for a given year (basically just a row count).
What I'm struggling to understand is - this formula doesn't seem to look at the SUMMARIZE table within it. If I put year and this measure in a matrix, it counts based on the raw table, not the grouped version built by SUMMARIZE in the formula. Any ideas why it's evaluating this way?
COUNTROWS(
SUMMARIZE(
Orders,
Orders[Customer ID],
"min_order_date_year",
MIN(Orders[Order Date].[Year])))
The formula is OK as per logic you provided to it. You have to clear about both your requirement and what your are writing in your code. Your formula is returning exactly what you are instructing it.
If I understand correct, you simply need the Customer count for the Minimum Year. For say if you have 6 unique customer for Year 2019 and 11 unique customer for Year 2020, you are looking here for value 6 to return by your measure.
Now, what your summarize table actually returning? if you create a separate custom table for the Summarize code only as below, you can see the table will actually hold all your customer name/id in first column and the second column will hole the MIN year available for that customer.
orders_summarize =
SUMMARIZE(
Orders,
Orders[customer id],
"min_order_date_year",MIN(Orders[Order Date].[Year])
)
So basically you have list of all customer in your summarize table. And now your are counting rows of your summarize table which is actually returning the total number of unique customers.
Finally, if you wants customer count for a specific Year (like MIN year), follow these below steps-
Step-1: Create a custom summarized table as below-
store_summarized_table =
SUMMARIZE(
store,
store[Customer ID],
"mindate",MIN(store[Order Date])
)
Step-2: create a measure as-
count_cust_id = COUNT('store_summarized_table'[Customer ID])
Step-3: Now configure your Matrix visuals as shown in the below image. You can also get the output in the image-
To avoid the Physical Table, you can do this below-
Step-1: Create a Custom Column as below-
is_min_year =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_year = store[Order Date].[Year]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_year_current_custommer_id =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_year = min_year_current_custommer_id, 1,0)
OR create a measure as-
is_min_year_measure =
VAR min_order_year_for_current_customer =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date].[Year]) = min_order_year_for_current_customer, 1,0)
Step-2: Create a Measure as below-
For created Custom Column
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
store[is_min_year] = 1
)
)
For Created Measure
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
[is_min_year_measure] = 1
)
)
Now add "Order Date" and measure "count_cust_for_min_year" to your Matrix. The out put will same as below-
I'm trying to count how many patients were in the hospital from one day to another and in which clinical department and specialty.
There is one table 'Patient Flow' with all patient's path during his hospitalization.
I was able to calculate this measure with another table 'Patient Entry-Out Date', where I filtered (ENTRY DATES < DATE REFERENCE) and (OUT DATES >= DATE REFERENCE or OUT DATES = null).
However, this way I can't identify what was the correct clinical department and specialty of this patient.
For example: the patient
03/13/2019 - shouldn't be counted
03/14/2019 - should be counted in clinical: UNIDADE CORONARIANA and specialty: CARDIOLOGIA
03/15/2019 - should be counted in clinical: UNIDADE CORONARIANA and specialty: CARDIOLOGIA
03/16/2019 - should be counted in clinical: CIRÚRGICA III and specialty: CARDIOLOGIA
03/17/2019 - should be counted in clinical: CIRÚRGICA III and specialty: CARDIOLOGIA
03/18/2019 - shouldn't be counted
I tried to create an index and get the maximum value between Admissions and Entry Transfer (table Last entry), but couldn't do it for each day.
Is it possible to do a summary in a filtered table(admissions and entries before) for each reference date ?
Or another solution?
Thanks in advance!
Pbix file https://www.dropbox.com/s/bqhyj4gihb5g4la/Daily%20hospital%20census.pbix?dl=0
Cássio A. Andrade
Here is a solution I was able to create. (working pbix can be downloaded from here)
1. Transformation
I used PowerQuery to transform original "Patient Flow" table into a new table (named "Patient Attendance") like below.
In this transformed table, each row represents a patient's attendance with a clinical department. A patient may enter a clinical department for an occurrence of either "Admission" or "Enter transfer", and may exit the department for either "Output transfer", "Medical release", or "Death". Each row has two time stamps; when the patient entered and exited the department.
Note, patients may be still staying with the department at the time when data is collected. In this case, exit time stamp for that patient is blank.
2. Modeling
I created a data model, which is a star schema composed of one fact table PatientAttendance illustrated above, and 3 dimension tables Clinics, Specialties, and Patients. Additionally, we have the Calendar dimension table, which does not have any physical relationship to other tables, but is used for measure calculation and slicing.
Then, I created the measure on top of this model which calculate how many patients were staying with the clinical department on any given day or period. The basic concept of the calculation logic is,
Look at each individual patient.
Look at each calendar date.
If the patient is staying with any department on the given day, add 1 count.
Patient-Days Stayed =
SUMX(
'Patients',
SUMX(
'Calendar',
IF(
CALCULATE(
COUNTROWS(
FILTER(
'PatientAttendance',
VAR CurrentDate = VALUES( 'Calendar'[Date] )
RETURN
'PatientAttendance'[Enter Timestamp].[Date] <= CurrentDate
&& (
ISBLANK( 'PatientAttendance'[Exit Timestamp] )
|| (
NOT( ISBLANK( 'PatientAttendance'[Exit Timestamp] ) )
&& 'PatientAttendance'[Exit Timestamp].[Date] > CurrentDate
)
)
)
)
) > 0, 1
)
)
)
The result looks to be working correctly (ignore too small numbers in early January, this is due to the sampling method of source data). If I am making any error, or if there is any suggestion of better approach, I am very interested to hear.