Add column with difference in days - powerbi

i'm trying the new Power BI (Desktop) to create a barchart that shows me the duration in days for the delivery of an order.
I have 2 files. 1 with the delivery data (date, barcode) and another file with the deliverystatusses (date, barcode).
I Created a relation in the powerBI relations tab on the left side to create a relation on barcode. 1 Delivery to many DeliveryStatusses.
Now I want to add a column/measure to calculate the number of days before a package is delivered. I searched a few blogs but with no succes.
The function DATEDIFF is only recognized in a measure, and measures seem to work on table date, not rowdata. So adding a column using the DATEDIFF function doesn't work.
Adding a column using a formula :
Duration = [DeliveryDate] - Delivery[OrderDate]
results in an error that the right side is a list (It seems the relationship isn't in place)?
What am I doing wrong?

You might try doing this in the Query window instead since I think each barcode has just one delivery date and one delivery status. You could merge the two queries into a single table. Then you wouldn't need to worry about the relationships... If on the other hand you can have multiple lines for each delivery in the delivery status table, then you need to get more fancy. If you're only interested in the last status (as opposed to the history of status) you could again use the Query windows to group the data. If you need the full flexibility, you'd probably need to create a Measure that expresses the logic you want.

The RELATED keyword is used to reference another table. Update your query as follows and it should work.
Like this:
Duration = [DeliveryDate] - RELATED(Delivery[OrderDate])

Related

Does Power BI support Incremental refresh for expanded table?

I have made table as follows (https://apexinsights.net/blog/convert-date-range-to-list):
In this scenario suppose I configure Incremental refresh on the Start Date column, then will Power BI support this correctly. I am asking because - say the refresh is for last 2 days or last 2 months, then it will fetch the source rows, and apply the transform to the partition. But my concern is that I will have to put the date param filter on Start date prior to non folding steps so that the query folds (alternatively power query will auto apply date filter so that query can fold).
So when it pulls the data based on start date and apply the transforms then I'm not able to think clearly about what kind of partitions it will create. Whether it is for Start date or for expabded date. Is query folding supported in this scenario?
This is a quite complicated scenario, where I would probably just avoid adding incremental refresh.
You would have to use the RangeStart/RangeEnd parameters twice in this query. Once that gets folded to the data source to retrieve ranges that overlap with the [RangeStart,RangeEnd) interval and a second time after expanding the ranges to filter out individual rows that fall outside [RangeStart,RangeEnd).

PowerBI - Draw Line Chart with X Axis Grouped by Date (in Days), When Column contains Full timestamp?

I've been trying to figure this for several hours now.
In my SQL table I have a full timestamp column, with date and time of the action, e.g. last time a user was logged in.
Say I have two users who logged in today so far, in different hours. I want to see "2" log ins for today's Date, instead of seeing them broken down further into exact timestamps.
See screenshot for example
What's the easiest way to do this?
EDIT:
Seems like the whole issues stems from my desire to use "Direct Query" method to load the data.
If I'm just importing the whole table (as users_table (2)), Then yes, I can create a new column with this syntax:
Column = 'users_view (2)'[last_active].[Date]
And plot the graph that I Want.
However, I am unable to create this column on direct query mode. Trying to create this column leads to an error:
Column reference to 'last_active' in table 'users_view' cannot be used
with a variation 'Date' because it does not have any.
If such basic functinoally is not possible, then what's the merit of Power BI? my use case is to create a REAL time dashboard.
Seems like if I want to have anything in real time, it means I can't build even the most basic graph, and vice-versa.
So either I'm missing something, or Power BI is worthless for real time reporting.
You need to follow these steps:
In your fact table, create a new column that corresponds to the date of the timestamp (this will be a grouping dimension column for your timestamps)
Add a measure that counts rows of your table
In a new chart, e.g. line chart, bar chart, whatever - use the date column as X-axis information, and the row count measure as your Y-axis information
Edit: Since it is now apparent you insist on using Direct Query for this - it would appear your data is not in the correct format to support what you want to do. The solution would be to provide Power BI with the correct format for the analysis you want to do. Like adding a column in your SQL database table.
A very costly way of calculating "something" useful would be to do a cumulative count based on timestamp that resets daily. This will look messy but for a site with a lot of activity it will be able to plot something. But it will be slow - since obviously we are far, far away from what would constitute a "sensible" use-case for Power BI using Direct Query.
Cumulative Daily Logins =
VAR _max_time = MAX ( users_table[last_active] )
VAR _date = DATEVALUE ( _max_time )
RETURN
COUNTROWS (
FILTER (
ALL ( users_table ) ,
users_table[last_active] <= _max_time
&& DATEVALUE ( users_table[last_active] ) = _date
)
)

Showing Side by Side Measures from Semi-Related Tables in Power BI

I have a data model in Power BI that, among other things, has the following tables
Employees (Dimension; employee ID/name)
Jobs (Dimension; contains details about the job, including job ID)
Employee history- Contains a record for each day an associate was in a job(snapshot table);
Job Budget History- Contains a record for each day a job was budgeted(snapshot table)
Calendar Table
The table is modeled like so (simplified version):
In Power BI, I am trying to make a simplified table view that contains measures based on both the budget history as well as the employee history for the most recent day in the dataset (simple count rows/distinct count of calendar table)
However, attempting to do so gives me the results below if I try to put both measures on the table. Basically it appears to be doing a cross join between each table and matching associates with jobs they don't have (this happens when the budget is added).
Of course, if I just do one of the singular measures everything works perfectly. I am fairly certain it is because there is no real connection between the 'employee' and the 'budget history' in this relationship, so it is just joining everything on the date without any context.
I have tried several things such as making inactive relationships with userelationship(), using visual level filters etc. but I'm not sure what the best option in this situation would be. (I am trying to avoid bidirectional relationships if possible)
Ideally this information should show on this date that Joe was present as President, Sally was present as an operator, and the Manager position had nobody, but all three were budgeted.
Any advice is appreciated. I have attached a simplified mockup pbix file for reference.
PBIX File
This is a complicated problem for many reasons. I was able to produce this report:
by removing field "Name" from the table and replacing it a measure:
Employee Name =
CALCULATE(
SELECTEDVALUE(Employees[Name]),
CROSSFILTER(Employees[Employee_ID], Employee_History[Employee_ID], BOTH)
)
It looks exactly like the report you want, but if you have additional requirements, you'll need to make sure that such approach works for you.
If this is acceptable, a brief explanation:
the root cause of the issue is missing Employee-Budget relationship. When you put Name in the table as a filter, it doesn't propagate to the budget table and causes a cartesian product.
Removing Name from the table eliminates the need for the filter propagation, but then you won't see employee names. I solved this by pulling employee names with the measure, where required propagation is forced by CROSSFILTER function (essentially, it's like a temporary bi-directional relation only when you need it, so it does not negatively affect the rest of the model).

Microsoft PowerBI Match multiple values is seperate table using ID and Date

I am making a demo IoT device monitoring dashboard. I can't understand which function should I use to check if devices are online on a certain date.
This is my sample report table1 from which I separate the Date to another column.
A device can report multiple times on a single day on the server. If it doesn't hit the server no report will generate.
Then I created a lookup table2 that contains all the device ID.
Now I created another table3 and generate a calendar date which I link with the table1 date.
Now In the column, I put my device ID and want to fill the column as true or false if the device reported on a particular date. I am unable to do it.
I used IF ( ISBLANK ( COUNTROWS ( RELATEDTABLE this function it didn't work
I want to create something like this. Which will look up the ID and date to report like it.
It will be a great help if anyone can share any idea.
The screen shots below give you everything you need.
There is a Device Connections table of the devices and when they connected. I have converted the DateTime to a date so that it can be joined to the Dates table, just a list of dates you want to check connections for. I have a relationship that connect the dates of the two tables.
Note: You could also preserve the time at which the device was seen if
needed. It is probably best to have it as a separate column. I have
discarded the time for simplicity.
I have created a single measure:
HasBeenSeen = IF (CALCULATE(COUNTROWS(DeviceConnections)) > 0, TRUE, FALSE)
Which gives a TRUE/FALSE if the device has been seen or not for whatever context exists (e.g. a given date). You could also just count the number of occurrences and display them.
Then I created a matrix visual with the Date from the dates table on the rows, Device ID on the columns and HasBeenSeen as the values to give the desired result.
As I said in the comments to you question, if you accept BLANK in cells where you shown FALSE, you can apply this simple steps. Only Table1 is sufficient for this and need no other table or joining.
Create a very simple Measure like below.
true_false = "True"
Now add a Metrics and configure it as below-
And here is the final output-

Change column values based on slicer selection

Using PowerBI desktop, I have created a small table (called TimeSelector), with three elements: Day, Week and Month
The idea is to use the content of this table to create a slicer with three options
Thus, selecting one of those options should change the way dates are used in tables.
For instance, selecting Day would result in the following table:
While selecting Week would result in this:
Etc..
I have tried to write a new measure taking in account the selected slicer element, but it is not working:
DayWeekMonthSelection = IF(CONTAINS(TimeSelector;TimeSelector[DayWeekMonth];"Month");
MONTH(VALUES('uptime_downtime'[Uptime_date])))
This is only the first part of the formula, only testing the month option as a start.
Any idea on how to do this?
To offer another perspective:
The approach I take with this is to have a separate table in the database - containing meta data about the date, called date_lookup.
2 of the fields in this table are FirstDateOfMonth & FirstDateOfWeek.
Some of the other fields are lastDateOfMonth & LastDateOfWeek, also DayOfWeek.
By using these fields I can easily present visuals that are grouped by month or week.
Sure you can use functions to get this information, but functions can be platform dependant. If you're making a join to the date_lookup anyway - it's no more effort to get this info from there...
The main reason we need to store this meta data is our company Financial year is Jul - Jun. Therefore we need to have available the Correct FY - which is stored as a field in the date_lookup table. I also have fields in there identifying public holidays...
This is an interesting question, but I'm not sure how to do exactly what you are asking for, so I'll suggest an alternative. (Changing a measure based on a slicer selection isn't too difficult, but I'm not sure a good way to swap out a field/dimension.)
Instead of creating a separate table for your slicers, a different possible approach would be to create a date hierarchy. Often when you drag a date column into the rows or columns box it will automatically create a date hierarchy with Year/Quarter/Month/Date, but since you want week and not quarter, let's create one manually.
First, create a couple calculated columns for week and month. For example:
Month = FORMAT(uptime_downtime[Date], "mmm")
Week = WEEKNUM(uptime_downtime[Date])
Now right-click on the date on the fields, and choose New Hierarchy. It should look like this now:
Now drag the Month and Week columns onto Date Hierarchy and then rearrange them in the appropriate order:
Now you can use that hierarchy in a matrix and use the drill up and down buttons
to get the different groupings: