Powerbi create mesaure from multiple tables - powerbi

I want to create a measure where i get the ammount of written hours in a shift. The issue is that my DAX knowledge is not sufficient to create such a formula.
The tables are as follows:
TABLE 1 (Shift information)
END | START | EMPLOYEE
datetime | datetime | varchar
TABLE 2 (Written time)
END | START | DURATION | EMPLOYEE
datetime | datetime | duration | varchar
Now i have created an simular SQL to create this data, unfortunately the SQL is so intensive that it would take too long to run it.
The formula in the SQL is as follows:
SUM(table2.duration) WHERE table2.end BETWEEN table1.start and table1.end
Any help would be appreciated!

Try DATESBETWEEN
=
CALCULATE (
SUM ( table2[duration] ),
DATESBETWEEN (
table2[end],
LASTDATE ( table1[start] ),
LASTDATE ( table1[end] )
)
)

Related

Find number of orders for customer first month

I am new to Power BI and learning how to perform cohort analysis with DAX in Power BI. Is there any way to find out how many times (or if easier if a customer buys more than a certain threshold) their first month?
If I have the table Customers:
ID | DateOfFirstRegistration
And the table of orders:
ID | customerId | orderDate
Let me know if any more information is helpful!
EDIT: If possible, is it also possible to plot it in a matrix with customers in each row and month 1 through 3 in the columns?
Thank you
You can do something like this measure in a table visualization together with customer ID:
First Month Purchases =
VAR _registration =
SELECTEDVALUE ( 'customers'[DateOfFirstRegistration] )
RETURN
CALCULATE (
COUNTROWS ( 'orders' ) ,
DATESINPERIOD (
'orders'[orderDate] ,
_registration ,
1 ,
MONTH
)
)

How do you reference a value within a previous filtered row in a DAX formula?

I am working on a fitness dashboard which provides a 'performance' score across different exercises.
In the provided screenshot, you can see a table which shows me an averaged daily 'performance' result for 'deadlifts' for example.
Obviously, I don't do these every day, so there are gaps between the dates for which I have records.
What I want is to be able to produce a '% improvement vs last recorded day' measure. Is it possible to create a measure that runs a comparison of this sort?
Assuming your table look like this
Table
+-------------------+-------------+
| Date | Performance |
+-------------------+-------------+
| 02 September 2020 | 20 |
+-------------------+-------------+
| 08 September 2020 | 20 |
+-------------------+-------------+
| 14 September 2020 | 20 |
+-------------------+-------------+
| 25 September 2020 | 32 |
+-------------------+-------------+
| 26 October 2020 | 36 |
+-------------------+-------------+
I have created a calculation that would scans the table for the last available date besides the
Steps:
Get the current value on each row. For example, 1st row is 2 of September. The value is stored in _SelectedValue
Use MAX to calculate the nearest value excluding the _SelectedValue. Save the result in MaxDate.
Using the 3rd step, filter the table to get the sum of performance for MaxDate.
Get the Percentage difference
Performance Calculation =
VAR _SelectedValue =
SELECTEDVALUE ( 'Table'[Date] )
VAR MaxDate =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Date] < _SelectedValue )
)
VAR PreviousPerformance =
CALCULATE (
SUM ( 'Table'[Performance] ),
FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Date] = MaxDate )
)
VAR CurrentPerformance =
SUM ( 'Table'[Performance] )
VAR Result =
IF (
ISBLANK ( MaxDate ),
BLANK (),
DIVIDE ( CurrentPerformance - PreviousPerformance, PreviousPerformance )
)
RETURN
Result
Output

Choose DAX measure based on slicer value

Is it possible to dynamically pick up appropriate DAX measure defined in a table by slicer value?
Source table:
+----------------+------------+
| col1 | col2 |
+----------------+------------+
| selectedvalue1 | [measure1] |
| selectedvalue2 | [measure2] |
| selectedvalue3 | [measure3] |
+----------------+------------+
The values of col1 I put into slicer. I can retrieve these values by:
SlicerValue = SELECTEDVALUE(tab[col1])
I could hard code:
MyVariable = SWITCH(TRUE(),
SlicerValue = "selectedvalue1" , [measure1],
SlicerValue = "selectedvalue2" , [measure2],
SlicerValue = "selectedvalue3" , [measure3],
BLANK()
)
But I do not want to hard code the relation SelectedValue vs Measure in DAX measure. I want to have it defined in the source table.
I need something like this:
MyMeasure = GETMEASURE(tab[col2])
Of course assuming that such a function exists and that only one value of col2 has been filtered.
#NickKrasnov mentioned calculation groups elsewhere. To automate the generation of your hard-coded lookup table, you could use DMVs against your pbix.
You might do something like below to get output formatted that can be pasted into a large SWITCH.
SELECT
'"' + [Name] + '", [' + [Name] + '],'
FROM $SYSTEM.TMSCHEMA_MEASURES

Dynamically rank a column based on filters

In my Power BI I have the following table
What I want is that when you create a graph and have a filter, the ranking column is modified. An example would be that when I change the ranking and select center, I get it like this:
site | region | nro_trabajos | ranking
----------+--------+--------------+------
OCCORO | centro | 58 | 1
PILCOMAYO | centro | 59 | 2
SICAYA | centro | 71 | 3
.
.
etc
I generate a table that gives me the following ranking. but has ties
the measure is rank_measure, as this image shows
that I can add in this measure so that the tiebreaker is with the alphabetical order of the "site" column and that it still has the region filter?
A calculated column cannot be dynamic with respect to slicers or filters, but you definitely can create a dynamic ranking measure.
ranking =
RANKX (
ALLSELECTED ( Table1 ),
Table1[nro_trabajos],
MAX ( Table1[nro_trabajos] )
)
Edit:
If you want to do tiebreaking, I think the simplest way is to first create a UniqueRank calculated column:
UniqueRank =
RANKX (
Table1,
FORMAT ( Table1[nro_trabajos], "0000" ) & Table1[region] & Table1[site]
)
You put each column that makes up a unique row in the order you want to rank by, formatting any numbers by padded with sufficient zeros so that the numeric ordering matches the alphabetic ordering.
Then simply use this calculated column UniqueRank instead of nro_trabajos in the measure I gave above:
ranking =
RANKX (
ALLSELECTED ( Table1 ),
Table1[UniqueRank],
MAX ( Table1[UniqueRank] )
)

how do I return values from a specific year for years in the future

I have two tables: a fact-table with hours spent, and a date dimensions table.
factTableHours:
| employeeName | hours | datekey |
dimDate: (from year 2000-2030)
| datekey | year | month | day |
I'm trying to create a simple estimate of how many hours an employee is going to spend in the future by looking at the hours he spent the last whole year.
It's similar to the SAMEPERIODLASTYEAR function, but i want a specific year based on today's date.So, if I did it today, I would like it to return the time registered in 2015, in order to get an estimate of how many hours he will spend in the coming years, 2016, 2017, 2018...
You can create a measure that calculates the last year hours.
Using the TODAY() function you will get the today's date, so we need to substract one year.
HoursLastYear =
CALCULATE (
SUM ( factTableHours[hours] ),
FILTER (
dimDate,
dimDate[datekey]
= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) )
)
)
Let me know if this helps.
Try something like this - With this Statement you get the figures from last year:
[PY Sales] = CALCULATE([Sales Amount]),DATEADD('Date'[Date],-1,YEAR)
BTW - here is a good reference for DAX Time Patterns:
http://www.daxpatterns.com/time-patterns/
I found a solution with the help of this page: https://www.sqlbi.com/articles/week-based-time-intelligence-in-dax/
I only needed another column in my time dimension, the day number of the year
hoursIn2015 :=
IF (
HASONEVALUE ( dimDate[year] ),
CALCULATE (
SUM ( factTableHours[hours] ),
FILTER (
ALL ( dimDate),
dimDate[year] = 2015
&& CONTAINS(
VALUES ( dimDate[Day number in year] ),
dimDate[Day number in year],
dimDate[Day number in year] )
)
),
BLANK ()
)