DAX - Production by Hour and By Machine - powerbi

I'm new to DAX and started with a problem of converting an old PDF report created in C# to paginated report on BI.
The data I have is similar to this:
Machine | ProductionTimestamp | ProductionHour
-----------------------------------------------------
M1 | 01/01/2022 06:35:12 | 06
M1 | 01/01/2022 09:30:12 | 09
M1 | 01/01/2022 22:55:12 | 22
M2 | 01/01/2022 09:55:12 | 09
M2 | 01/01/2022 22:35:12 | 22
M3 | 01/01/2022 07:35:12 | 07
M3 | 01/01/2022 08:35:12 | 08
I need generate a daily paginated report with this information: hourly production by machine, between 04h and 0h.
Production is the count of ProductionTimestamp in each hour of day.
ProductionHour | M1 | M2 | M3
04 | 0 | 0 | 0
05 | 0 | 0 | 0
06 | 1 | 0 | 0
07 | 0 | 0 | 1
08 | 0 | 0 | 8
09 | 1 | 1 | 0
10 | 0 | 0 | 0
...
22 | 1 | 1 | 0
23 | 0 | 0 | 0
To the moment, I have the hourly production for 1 machine, but I don't know how to go ahead and add a column per machine.
Thanks for any help.
EVALUATE
SUMMARIZECOLUMNS (
'FACT-ProducaoBanburys'\[HoraProducao\],
'FACT-ProducaoBanburys'\[Maquina_ID\],
FILTER (
VALUES ( 'FACT-ProducaoBanburys'\[DataProducao\] ),
'FACT-ProducaoBanburys'\[DataProducao\]
= TODAY () - 1
),
FILTER (
VALUES ( 'FACT-ProducaoBanburys'\[Maquina_ID\] ),
'FACT-ProducaoBanburys'\[Maquina_ID\] = 1
),
"M1", COUNT ( 'FACT-ProducaoBanburys'\[Data\] )
)
ORDER BY 'FACT-ProducaoBanburys'\[HoraProducao\]
And the result is:
HoraProducao | M1
6 | 4
7 | 12
8 | 16
9 | 12
10 | 24
...

You can start by creating a hour table using create new table:
Hour = SELECTCOLUMNS(UNION(GENERATESERIES(04,23,01),{00}),"Hour",[Value])
Your resulting table:
Then create a one-many relationship with your fact table, like this on the model view:
Now Come to your main screen, and write this dax codes for each and every one of your Machine Code:
For M1 Machine
M1 = CALCULATE(
COUNT(YourTbl[ProductionTimestamp]),YourTbl[Machine]="M1")
For M2 Machine
M2 = CALCULATE(
COUNT(YourTbl[ProductionTimestamp]),YourTbl[Machine]="M2")
For M3 Machine
M3 = CALCULATE(
COUNT(YourTbl[ProductionTimestamp]),YourTbl[Machine]="M3")
To finish the task, create a table visual:
Put [Hour] Column on your created hour table into row, and above measures next to values area: The result:

Related

How to show percentage measure in a card visual?

Sample data:
+----------+------------+----------+-----------+
| PersonID | Date | Booked | Picked |
+----------+------------+----------+-----------+
| 1 | 1 Jan 2023 | 100 | 100 |
| 2 | 1 Jan 2023 | 40 | 30 |
| 3 | 1 Jan 2023 | 20 | 40 |
| 1 | 2 Jan 2023 | 50 | 80 |
| 2 | 2 Jan 2023 | 70 | 70 |
| 3 | 2 Jan 2023 | 60 | 40 |
+----------+------------+----------+-----------+
I have a measure as follows:
Performance % = DIVIDE(IF(Calls[Picked]>Calls[Booked],Calls[Booked],Called[Picked]),Calls[Booked])
I have formatted this as %
When I place this in a table visual then I get a % value.
But when I place it into a card visual then it forces me to choose sum/min/max/...
What is the way to display the value of a measure in a card visual?
How to iterate over each row to calculate the percentage value - for example - there is no DIVIDEX in dax.
Can you please try with a Measure as below-
Performance % =
DIVIDE(
IF(
SUM(Calls[Picked])>SUM(Calls[Booked]),
SUM(Calls[Booked]),
SUM(Calls[Picked])
),
SUM(Calls[Booked])
)

Computing a sequence of events over time and extract a percentage of duration

I have a dataset which stores events regarding the availability status of a room.
For example, if someone is entering the room at 8:30 am, I get the following row in my table :
# room status date
--- ---- -------- -------------------
0 A1 OCCUPIED 2022-01-01 08:30:00
A similar event is created when this person is leaving the room. My table would then look like this :
# room status date
--- ---- --------- -------------------
0 A1 OCCUPIED 2022-01-01 08:30:00
1 A1 AVAILABLE 2022-01-01 09:15:00
In practice, the table has way more entries, and data are intertwined.
# room status date
--- ---- --------- -------------------
0 A1 OCCUPIED 2022-01-01 08:30:00 <--
1 B4 OCCUPIED 2022-01-01 08:32:00
2 C2 OCCUPIED 2022-01-01 08:41:00
3 A1 AVAILABLE 2022-01-01 09:15:00 <--
4 C2 AVAILABLE 2022-01-01 09:20:00
5 A1 OCCUPIED 2022-01-01 09:30:00 <--
6 B4 AVAILABLE 2022-01-01 10:00:00
7 A1 AVAILABLE 2022-01-01 12:00:00 <--
I am currently looking for a way to extract a percentage/duration of availability from each of my rooms, but I don't know how to proceed.
I have created a few measures :
// A measure to count the total of status
Count status = COUNT(myTable[status])
// A calculated measure for available ones
Total available = CALCULATE([count status], myTable[status]=="AVAILABLE")
// A calculated measure for occupied ones
Total occupied = CALCULATE([count status], myTable[status]=="OCCUPIED")
I already have a date hierarchy which means I can change the granularity from year to month, to week day, to hour of the day. I can also apply a filter to select a range of hours, for example 8:00 to 18:00.
The problem is, the measures I have created simply count the number of changes that occur in a given period (in the chart below, the hours), but they don't reflect the actual duration of each event, which means that my graph is actually wrong.
If I take my room A1 as an example, in the actual configuration, my graph would look like this :
___ ___ ___ ___ ___ ___ ___ ___
| 0 | | | | | | | |
available | | 50| | |100| | | |
| |___| | | | | | |
|100| | | | | | | |
occupied | | 50| | | 0 | | | |
|___|___|___|___|___|___|___|___|
8 9 10 11 12 13 14 15
In the column 8, 100% occupied because 1 entry in the dataset for this status vs 0 entry for "available".
In the column 9, 50-50 because 1 entry for each status (one at 09:15, the other at 09:30)
...
The result I am looking for is this one :
___ ___ ___ ___ ___ ___ ___ ___
| | 25| 0 | 0 | | | | |
available | 50|___| | |100|100|100|100|
|___| | | | | | | |
| | 75|100|100| | | | |
occupied | 50| | | | 0 | 0 | 0 | 0 |
|___|___|___|___|___|___|___|___|
8 9 10 11 12 13 14 15
In the column 8, I would get 50-50 because the room was available between 08:00 and 08:30, but then it was occupied
In the column 9, I would get 75% occupied because the room was only available between 09:15 and 09:30
In the column 10, I would get 100% occupied
...
Is it possible to get it through a DAX measure or do I need to restructure some of my data ?
The solution to your problem is to add calculated column to your source table which has the time of next Event in the same room. The Room_No here is your category column.
First, add index by category (by Room)
Event_asc =
VAR Current_Category = Table[Category]
RETURN
RANKX (
FILTER (
Table,
Table[Category] = Current_Category
),
Table[DateTime], , ASC, Dense
)
Then add this column:
Event_Next_Time =
VAR Current_Category = Table[Category]
VAR CurIndex = Table[Event_asc]
VAR Result =
CALCULATE(
MAX( Table[DateTime] ),
Table[Category] = Current_Category
&& Table[Event_asc] = CurIndex + 1,
REMOVEFILTERS()
)
RETURN
Result
Once you have it, just add a third column which calculates the difference between two Datetimes (Event and NextEvent).
Lapse = DATEDIFF( Table[DateTime], Table[TimeOfNextEvent], SECOND )
The rest should be easy for you :-)

How to extract time series data before and after a specific event by using AWS athena?

I have the storage that stores huge time series data and I can extract data through AWS athena.
However, I don't have any ideas how I can extract time series data before and after a specific event by using AWS athena.
What query can achieve it ?
Does anyone have ideas and samples of query on Athena ?
For example, I have following input data.
<input data>
id | timestamp | value | level |
---------------------------------------------
1 | 2021-04-01T12:00:00+00:00 | 100.0 | 1 |
2 | 2021-04-01T12:00:10+00:00 | 98.0 | 1 |
3 | 2021-04-01T12:00:20+00:00 | 99.5 | 1 |
...
58 | 2021-04-01T12:09:40+00:00 | 98.2 | 1 |
59 | 2021-04-01T12:09:50+00:00 | 95.3 | 1 |
60 | 2021-04-01T12:10:00+00:00 | 99.2 | 1 |
61 | 2021-04-01T12:10:10+00:00 | 97.6 | 2 |
62 | 2021-04-01T12:10:20+00:00 | 98.6 | 2 |
63 | 2021-04-01T12:10:30+00:00 | 98.3 | 2 |
64 | 2021-04-01T12:10:40+00:00 | 98.1 | 2 |
...
100 | 2021-04-01T12:16:40+00:00 | 97.6 | 2 |
What I want to do is to extract the records for 30 sec before and after level 1->2 change event.
In this case, the expected outputs are data from id:58 to id:64.
You can use 'lag' function to determine timestamps where the level changes:
SELECT *
FROM (SELECT *
FROM (SELECT timestamp,
lag(level) OVER (order by timestamp) AS prev_level,
level
FROM dataset)
WHERE prev_level != level)
And then use those timestamps to filter out the dataset. For example something like this:
WITH dataset(id,timestamp,value,level) AS (
VALUES
('1',timestamp '2021-04-01 12:00:00+00:00',100.0,1),
('2',timestamp '2021-04-01 12:00:10+00:00',98.0,1),
('3',timestamp '2021-04-01 12:00:20+00:00',99.5,1),
('58',timestamp '2021-04-01 12:09:40+00:00',98.2,1),
('59',timestamp '2021-04-01 12:09:50+00:00',95.3,1),
('60',timestamp '2021-04-01 12:10:00+00:00',99.2,1),
('61',timestamp '2021-04-01 12:10:10+00:00',97.6,2),
('62',timestamp '2021-04-01 12:10:20+00:00',98.6,2),
('63',timestamp '2021-04-01 12:10:30+00:00',98.3,2),
('64',timestamp '2021-04-01 12:10:40+00:00',98.1,2),
('100',timestamp '2021-04-01 12:16:40+00:00',97.6,2)
)
SELECT *
FROM dataset o
WHERE EXISTS(
SELECT *
FROM (SELECT *
FROM (SELECT timestamp,
lag(level) OVER (order by timestamp) AS prev_level,
level
FROM dataset)
WHERE prev_level != level)
WHERE (o.level = level AND o.timestamp - timestamp < interval '30' second)
OR (o.level = prev_level AND timestamp - o.timestamp < interval '30' second)
)
Output:
id
timestamp
value
level
59
2021-04-01 12:09:50.000 UTC
95.3
1
60
2021-04-01 12:10:00.000 UTC
99.2
1
61
2021-04-01 12:10:10.000 UTC
97.6
2
62
2021-04-01 12:10:20.000 UTC
98.6
2
63
2021-04-01 12:10:30.000 UTC
98.3
2

Power BI Dax calculate the Sum% of Top 10 products

UserRequest
I want to get the Sum of top 10 %, ie. the third column which is % of
Value / total value
x
Sample Data##
| ProductName | value | Ranking | |
A1 | 10 | 1 | 10%
A2 | 8 | 2 | 8%
A3 | 6 | 3 | 6%
A4 | 4 | 4 | 4%
A5 | 2 | 5 | 2%
A6 | 1 | 6 | 1%
A7 | 1 | 7 | 1%
A8 | 1 | 8 | 1%
A9 | 1 | 9 | 1%
A10 | 1 | 10 | 1%
A11 | 1 | 11 | 1%
Total value = 100
Expected outcome The measure should return sum of Top 10% = 35%
Here is the Dax I have built so far.
Eq_TopN% =
VAR RANKMV = [RANKMV]
VAR Top_N = 10
//VAR RANKMV = RANKX(ALL(Eq_Bucket_tbl[Issuer]),Eq_Measures_tbl[MaxMV],,DESC,Dense)
RETURN
SUMX(TOPN(Top_N,CALCULATETABLE(ALLSELECTED(Eq_Bucket_tbl),FILTER(Eq_Master_Dates,Eq_Master_Dates[Date]=[MaxDateSelected])), [Total MV], ASC), [Total MV])
RANKMV = RANKX(
ALL(Eq_Bucket_tbl[Issuer]),
Eq_Measures_tbl[MaxMV]
,
,,Dense
)
If you have the ranking column then just use calculate:
SumOfTOP = CALCULATE(sum(Sheet1[ % ]), Sheet1[ Ranking ] <= 10)
You can create a measure to calculate the sum of percentages for the top 10 values.
Sum of TOP10% =
(
CALCULATE (
SUM ( 'Table'[Value] ),
TOPN ( 10, 'Table' )
) / 100
)
After creating the measure, go to measure tools by clicking on the measure, and then in the formatting section set the measure format to Percentage.
This should give you the desired result.

Power BI: Count rows of a table based on a criteria of another table

I have a table resuming the list of apps by month and year like this:
Table 1
App | Month | Year
A | 1 | 2019
B | 1 | 2019
C | 1 | 2019
D | 1 | 2019
E | 1 | 2019
...
etc
And I have a table that contains the info of all the tickets of the company, including the App and the Month and Year, like this:
Table 2
IDTicket | App | Month | Year
44424 | B | 1 | 2019
44425 | D | 1 | 2019
44426 | B | 1 | 2019
44427 | A | 1 | 2019
44428 | E | 1 | 2019
...
etc
I need to add to the Table 1, a column that count the amount of tickets of Table 2 according to each app, month and year, like this:
Table1
App | Month | Year | CountOfTickets
A | 1 | 2019 | 1
B | 1 | 2019 | 2
C | 1 | 2019 | 0
D | 1 | 2019 | 1
E | 1 | 2019 | 1
Try something like this:
CountOfTickets =
CALCULATE (
COUNT ( Table2[IDTicket] ),
Table2[App] = EARLIER ( Table1[App] ),
Table2[Month] = EARLIER ( Table1[Month] ),
Table2[Year] = EARLIER ( Table1[Year] )
)