I've got the following table in Power BI:
Date | PersonID | Hours | Age
------------------------------|------
02-jan-18 | 4 | 8 | 3
06-jan-18 | 4 | 6 | 3
01-feb-18 | 4 | 6 | 3
05-feb-18 | 4 | 4 | 4
01-jan-18 | 5 | 6 | 3
01-feb-18 | 5 | 6 | 3
I have rows of data up until a few years back for multiple PersonID's. Most people have multiple rows per month because the data is split out on separate days. For every date, I have that person's age at the time (in this case, PersonID "4" had a birthday between feb 1st and feb 5th).
What I want to do is calculate the amount of hours PER MONTH, PER AGE. My end result should look something like this (average hours per month shown per age):
Age | Average hours per month
----------------------------------
1 | 35
2 | 31
3 | 28
4 | 28
I have no idea how to get started. How can I calculate a sum over 2 columns?
First, create a column on your table that will allow you to group by month:
MonthYear = EOMONTH(HoursAge[Date], 0)
Now you can write a measure that takes an average over a summarized table:
AvgHoursPerMonth = AVERAGEX(
SUMMARIZE(HoursAge,
HoursAge[MonthYear],
HoursAge[Age],
"MonthHours", SUM(HoursAge[Hours])),
[MonthHours])
Here's what the summarized table looks like for your given example:
This would give the following result when you put the measure into a table with age on the rows:
Age | AvgHoursPerMonth
----|-----------------
3 | 16
4 | 4
Related
will be very grateful if you could share your experience and advice on the following problem in Power BI:
3 Tables given in the data model:
calendar dimension table
fact table on sessions
fact table on spending
| CW | Total cost | Sessions | Expected Column 1 | Expected Column 2 |
+----+-------------+-----------+-------------------+-------------------+
| 1 | 1200 | 50 | | |
| 2 | 1500 | 60 | 1200 | 50 |
| 3 | 1700 | 48 | 1500 | 60 |
| 4 | 1150 | 36 | 1700 | 48 |
| 5 | 900 | 29 | 1150 | 36 |
+----+-------------+-----------+-------------------+-------------------+
CW column indicates the calendar week and it is from calendar table. Sessions and Total cost are from sessions and spending tables respectively. Data is aggregated and visualized on calendar week level.
Problem: I need to create measures to derive Expected column 1 and expected column 2 based on total cost and sessions columns. Basically getting next values for each row similar to lead window function.
I have checked power BI community and there are several ideas (for example here https://community.powerbi.com/t5/Desktop/DAX-Query-to-Find-Next-Value/td-p/833896).
But these solution assume all columns are from the same table, however in the above described case
all 3 columns are from different tables.
Will the be possible to get expected columns 1 and 2 and how? Many thanks in advance!
My data looks like this :
Item | Packaged Date | Delivery Date | Days took
1 | 17-05-2019 | 19-05-2019 | 2
2 | 23-05-2019 | 24-05-2019 | 1
3 | 22-05-2019 | 30-05-2019 | 8
I want to make a table using DAX where i have two columns
Number of Days | Items
0-5 | 2
5-10 | 1
This basically means within 5 days, 2 items in total were sold
and within 5 or 5-10 days , 1 item was sold
I found a way using DAX expression to solve the my own Question.
I created a DAX Query :
AggregatedDays = IF(Dates[Days]<=5 && Dates[Days]>=0 , "0-5 Days","5-10 Days")
A new table is created using Aggregated Days column and Items with items being "sum" from the "VALUES" table.
I am trying to transpose three columns by two variables.
My current dataset looks like:
Person Date Company Industry Number
John 2017 Apple Tech 5
John 2017 Starbucks Beverages 3
Kim 2014 Hilton Hotels 9
I would like my output data set to look like:
Person | Date | Company1 | Industry1 | Number1 | Company2 |Industry2| Number2
John | 2017 | Apple | Tech | 5 | Starbucks| Beverage| 3
Kim | 2014 | Hilton | Hotels | 9 | - | - | -
As you can see, I would like each observation to be unique by name and date.
Any suggestions?
I am trying to accomplish something, but don't know how to do it.
I have a Dimension (Table called TEntry) that represents time entries for employees like so :
Id | EmployeeId | EntryDT | TimeInMinutes | PriceAgreementId
------ | ---------- | ---------- | ------------- | ----------------
1 | 1 | 2017-03-20 | 100 | 1
2 | 1 | 2017-03-31 | 50 | null
3 | 2 | 2017-03-21 | 100 | 1
4 | 2 | 2017-03-23 | 125 | 2
5 | 3 | 2017-03-15 | 90 | null
6 | 3 | 2017-03-25 | 60 | 1
Sometimes they work on "PriceAgreements", and sometimes they don't.
In my Dashboard, i have a Table that groups the table TEntry by EmployeeId and Sums the TimeInMinutes. I also have a Slicer for EntryDT :
EmployeeId | TimeInMinutes
-------------- | -------------
1 | 150
2 | 225
3 | 150
I need to create 2 new columns that represent :
The total TimeInMinutes an Employee has worked on all PriceAgreements
So for EmployeeId #1, the Total would be 100.
The total TimeInMinutes ALL Employees have worked, but only for the PriceAgreements the current Employee (current row) has worked on.
The Table would look like this (without the PriceAgreementIds in parenthesis) :
EmployeeId | TimeInMinutes | TimeInMinutes on PriceAgreements | TimeInMinutes on PriceAgreements ALL other EmployeeIds
-------------- | ------------- | -------------------------------- | ------------------------------------------------------
1 | 150 | 100 (PriceAgreementId=1) | 260 (PriceAgreementId=1)
2 | 225 | 225 (PriceAgreementId=1 and 2) | 385 (PriceAgreementId=1 and 2)
3 | 150 | 150 (PriceAgreementId=1) | 260 (PriceAgreementId=1)
Column "TimeInMinutes on PriceAgreements" is quite easy, but the other one, i cannot find a solution...
I have this DAX expression I started, but it is not complete:
CALCULATE(SUM(TEntry[TimeInMinutes]), NOT ISBLANK(TEntry[PriceAgreementId]), ALL(TEmployee))
TEmployee is a Dimension linked to the main TEntry Table.
Any help would be appreciated.
Thank you
I'm throwing this on as an answer because (a) it might get you (or someone else) going in the right direction and (b) if it's guaranteed that an Employee would only ever have time entries corresponding to 2 price agreements, this would work - which is unlikely the case for you, but might be the case for others trying to accomplish a similar thing.
Measure =
CALCULATE (
SUM ( TEntry[TimeInMinutes] ),
FILTER (
ALL ( TEntry ),
(
TEntry[PriceAgreementID] = MIN ( TEntry[PriceAgreementID] )
|| TEntry[PriceAgreementID] = MAX ( TEntry[PriceAgreementID] )
)
&& TEntry[PriceAgreementID] <> BLANK ()
)
)
This measure is saying: SUM the TimeInMinutes for all records in the TEntry table where the PriceAgreementID matches either the minimum OR maximum PriceAgreementID (in the context of the current row) AND the PriceAgreementID isn't blank.
The fatal flaw in this answer is in the MIN and MAX. For Employee ID 2, who has 2 PriceAgreementIDs (1 & 2) - the MIN will calculate the minutes for PriceAgreementID 1 and the MAX will calculate the minutes for PriceAgreementID 2. However, to expand to a case where there might be more than 2 PriceAgreements...I don't know how to do that.
It does work on the sample data in your question, though (since there is a max of 2 price agreements per employee):
Typically when I'm faced with a problem like this that isn't easy to solve, I think about my data model and make sure that it conforms to a star schema as closely as possible.
In your case, an employee can have multiple price agreements, and a price agreement can be associated with many employees. That, to me, suggests a many-to-many relationship. I'd strongly recommend reading more about many-to-many relationships and whether restructuring the underlying tables (e.g. to include a bridge table) would help get you closer to the answer you need.
A good starting point might be: https://www.sqlbi.com/articles/many-to-many-relationships-in-power-bi-and-excel-2016/
I have a table in my Django app, UserMonthScores, where every user has a "score" for every month. So, it looks like
userid | month | year | score
-------+-------+------+------
sil | 9 | 2014 | 20
sil | 8 | 2014 | 20
sil | 7 | 2014 | 20
other | 9 | 2014 | 100
other | 8 | 2014 | 1
I'd like to work out which position a specific user was in, for each month, in the ranking table. So in the above, if I ask for monthly ranking positions for user "sil", per month, I should get a response which looks like
month | year | rank
------+------+-----
9 2014 2 # in second position behind user "other" who scored 100
8 2014 1 # in first position ahead user "other" who scored 1
7 2014 1 # in first position because no-one else scored anything!
The way I'd do this in SQL is to join the table to itself on month/year, and select rows where the second table was for the specific user and the first table had a larger score than the second table, group by month/year, and select the count of rows per month/year. That is:
select u1.month,u1.year,count(*) from UserMonthScores u1
inner join UserMonthScores u2
on u1.month=u2.month and u1.year=u2.year
and u2.userid = 'sil' and u1.score >= u2.score
group by u1.year, u1.month;
That works excellently. However, I do not understand how to do this query using the Django ORM. There are other questions about joining a table to itself, but they don't seem to cover this use case.