Create a monthly graph containing dynamical values - powerbi

I discovered PowerBI few hours ago and I am trying to render a new graph, pretty complex from my data.
My subject:
I have a list of employees from my society with some information (name, location, seniority, ....) inside a table named Suivis de mission.
I'm focusing on seniority column. I have to display, monthly, the number of employees which has less than 3 seniority years, more than 3, more than 5 and more than 8.
This field is calculed through another field (date_de_changement_de_mission) by the next formula:
(today - date_de_changement_de_mission) / 365
It gives me a number according to the seniority years (above in french)
My experimental work:
I tried to make some things and this is what I have up to now.
I created a new table with months (string column1 and int column2) and the third column according to the number of employee with less than 3 seniority years for each month.
This is the formula that I'm trying to implement in order to get the number but I have lot of mistakes :
Nbr_inf_3_ans = SUMX('Suivis de mission';
IF(
'Suivis de mission'[Activité] = "En poste";1 &&
DIVIDE(
DATEDIFF(01/01/2019;'Suivis de mission'[Date de changement de mission].[Date];DAY);365)
;"null"))
The formula must contains several conditions :
Field 'Suivis de mission'[Activité] has to be : "En poste"
Loop over each month in my table (if the month is not targed, display 0)
I'm a bit lost.

Assuming that:
You want to do it in Power Query Editor
You already created the "seniority" and "month" ("mois*") columns
You are not worried about the year (you are really grouping just by month)
Then these steps may help you to begin:
Create a custom column name "IsLesserThan3" with the formula:
if [Seniority] < 3 and [Activité] = "En poste" than 1 else 0
Change the formula to include the requirement about "if the month is not targed" (i didn't get what it means, sorry).
Choose "Transform" (menu), then "Group By", then basic, set group by = month, give it a name ("Nbr_inf_3_ans"), set operation = sum, set column = IsLesserThan3
EDITED
New extra assumption:
There is a second table with month description and month number and you want to create the new column there ("Nbr_inf_3_ans") and fill it, probably to grant all the months are there (including those with no occurrences).
With this new assumption in mind, the approach will be to join the two tables and do the same thing:
Create a custom column named "IsLesserThan3" with the formula:
if [Seniority] < 3 and [Activité] = "En poste" than 1 else 0
Change the formula to include the requirement about "if the month is not targed" (i didn't get what it means, sorry).
Change the type of the new column to integer (click the icon to the left of the column name).
Create a custom column named "Mois_int" with the formula:
Date.Month([date_de_changement_de_mission])
Then join the tables by month. To do that, select the other table and choose "Transform" (menu), then "Merge", then let the second table selected and select the first table as the "bottom" table, select columns "Mois_int" in both tables, set "join kind" = "left outer".
PowerBI will create a new column wich name is the name of the first table. Click the arrows icon in that column header and choose "aggregate" and mark only "sum of LesserThan3".

Related

PowerBi Join column value strings together in each row

I have a table like shown below titled Employees:
Name LOC1 LOC2 LOC3 LOC4
-----------------------------------------
Joe DAY CVG DTW
Jane PVD STL
John LAX SAN SFO ANC
I created a measure to combine the LOC fields into 1 result separated by a space. I keep getting errors because PBi wants to aggregate the data fields. All fields are type Text. I've tried all the usual combine methods, below are a couple of examples...
CombineLOCs=CONCATENATE('Employees'[LOC1],'Employees'[LOC2])
CombineLOCs= 'Employees'[LOC1] & " " & 'Employees"[LOC2]
This is what I get back:
"A single value for column 'LOC1' in table 'Employees' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation....etc."
I ultimately have a table visual on my canvas and instead of listing out all of the columns on that table, I just want 1 column with all of the combined LOC values for each employee in it.
If you just want a single column where the LOC columns are joined together, then you need to create a new column within the table, rather than a new measure. A measure expects you to aggregate the values. A column will concatenate the text strings on a row by row basis.
If you select the new column option, either on the table tools or the fields sidebar, then use the concatenate formula in your question, that should work.
With the new column, you can then include the employee name and the associated LOC values.

PowerBI - conditional formatting for highlighting weekends

I have a data table matrix in power bi showing Dates in Rows and hours in columns and count of customers against them as values. i wanted to highlight all rows of thursday, friday and saturday, so that weekend figures can be compared.
Need help in this pls...
the table is in the below format
enter image description here
First you want to create a duplicate (in my case monday - friday (2) that does NOT have a relation to you source table. You want this duplicate table to only show days. This duplicate table needs a relationship with the slicer you are going to use to filter for days
In your source table (monday - friday) you want to create the following measure:
Measure = IF(HASONEVALUE('monday -friday (2)'[Day]);
IF(SELECTEDVALUE('monday -friday (2)'[Day]) = MAX('monday -friday'[Day]); 1; 0);
IF(ISFILTERED('monday -friday (2)'[Day]) && COUNTROWS(FILTER('monday -friday (2)'; 'monday -friday (2)'[Day] = MAX('monday -friday'[Day]))); 1; 0))
Now you want to create conditional formatting. First create your table with days and values (for the example i did not sort the table. You want to sort your table so it looks better):
Then right click on values and select conditional formatting. Select based on rules.
and the measure you just created in your source table.
for the first option you select "is bigger than or equal to" and give it a value 1.
for the second option you select "is smaller than"and give it a value 1000.
Choose the color you want your highlights to be.
If you now select a day in your filter the value for that day will show the background color you selected! In your case you want to select all days you want to have highlighted.
Hope this helps!

Create Calculated Column Based on Teams with Four Wins in Final Round Utilizing DAX

I need a calculated column for champions and runner up. To become a champion, a team must attain 4 wins in a given year in the final round. I just don't know how to translate that for DAX. I want to be able to have a Year Slicer that will show the Champion and Runner Up for a given year in a card.
I have tried a summary table and using TOPN, but since the count of finalists are all one, I am getting no luck. In the picture below, I would like a column delegating if a team is a champion or runner up
Create a new column by clicking in the Modeling tab and selecting 'New Column". Using the code below I created a new column which takes the column which contains the 'Count of Champions' and IF the number is greater than or equal to 4 then "Champion" is placed in the adjacent column, if the number is below 4, then "Runner Up" is placed in the adjacent column.
Status = IF(Table1[Count of Champions] >= 4, "Champion", "Runner Up")

Split single row of data in Power BI data source into multiple rows

I have a table in a Power BI data source with a column for term start and term end date (term length can be longer than a month), along with meta data on the term. I need to report on status of purchased terms as at the end of each month. As far as I can see, the best way of accomplishing this would be to create a calculated table with an entry for each month on which a term is active at its end.
For example, an entry in the original table with the following data:
TermStartDate TermEndDate PurchaseAmount
2018-01-03 2018-04-12 100
Would end up in the calculated table as follows:
MonthPurchased PurchaseAmount
2018-01 100
2018-02 100
2018-03 100
How to accomplish this? Is there a better way than creating a separate calculated table to get this data? Any help or advise is appreciated
I managed to solve this myself, I detail the required steps below for reference:
Change start and end date column data types from Datetime to Date. <- This is needed to ensure we only generate dates on day boundaries in the next step
Add custom column with the following formula:
Month = List.Select( List.Dates([TermStartDate], Number.From([TermEndDate] - [TermStartDate]) +1, #duration(1, 0, 0, 0)), each _ = Date.EndOfMonth(_) )
This generates a list of dates between start and end, then filters to only leave the dates that are at the end of a month
Expand to new rows on the new Month column (menu at the top of the column)
Use Detect Data Type option on the Month column to change the datatype from Any to Date (for some reason I cannot manually select Date, the DataType menu option is greyed out on the Month column)

PowerBI - Time Lag Calculation in Dates, using Query Editor

I'm new to Power BI Desktop, coming from Excel.
In the query editor, I would like to create a new column in a table with the difference in time/date from one record to the next, by a separate grouping column (device). An example explains it better. Here's the starting point for the data, with one column for the device id, and another for the Date of the event.
Device Date
A 5/1/2016
B 5/1/2016
C 5/2/2016
A 5/4/2016
B 5/5/2016
A 5/10/2016
B 5/9/2016
C 5/12/2016
I would like to group by Device and Sort by Date, then calculate the differences, to make something like this:
Device Date Lag
A 5/1/2016 (null)
A 5/4/2016 3
A 5/10/2016 6
B 5/1/2016 (null)
B 5/5/2016 4
B 5/9/2016 4
C 5/2/2016 (null)
C 5/12/2016 10
What's the best way to do this in Power BI query editor?
Thanks for the help!
Here's a solution. But it does not involve Query editing.
we're gonna create a calculated column and do it the dax way.
1) In your Fields pane, right click on the table and Select "New
Column"
2) In the formula bar, type in the formula below. replace TableName with your table name
LagColumn = DATEDIFF(TableName[Date]
, CALCULATE(MAX(TableName[Date]),
FILTER(TableName,
TableName[Device] = EARLIER(TableName[Device]) &&
TableName[Date] < EARLIER(TableName[Date])
)
)
, DAY
)
3) Select Device, Date and the LagColumn in your report now. Choose "Table" option from the visualization panel.
Explanation of the formula -
1) The new lagColumn is the DATEDIFF in days of two entities.
2) First argument is the DATE field of the current row
3) Second argument is the maximum date value of all the dates that are less than the current row's date and that belongs to the same device as the current row. (EARLIER helps you retrieve the value of the current row in the previous context.)
read more here
1) EARLIER - https://msdn.microsoft.com/en-us/library/ee634551.aspx
2) Row Context and Filter Context - https://www.sqlbi.com/articles/row-context-and-filter-context-in-dax/
Screenshot of a similar use-case:
Here,
AccountAlternateKey corresponds to Date
ParentAccountAlternateKey
corresponds to Device, and
LagColumn is LagColumn
Note - For every group, lag of the first column is the first column itself. If you want it to be NULL, you can check if ISBLANK(CALCULATE....) is true and then make it null. that just adds a little bit complexity to the formula..
Proposing DAX solution since i don't think there is a query way to deal with this, AFAIK.
Let me know if you have any more questions..