Creating a summarizecolumns filling blank data with zero - powerbi

I'm kind of new with DAX and I'm currently stuck with a problem.
I have two tables Fiscal Calendar and Customer. I created a relationship using the date columns from both table.
The customer table contains records and I would like to show on a matrix if a record exist or not on specific dates (or fiscal weeks) like in the image below.
I made a summarizecolumns table (see the dax sentence below) however, what I need is if the record doesn't exist on specific date/week, I want to fill it with zero value instead of having it just blank. I tried many things already but until now I couldn't find any solution. If anyone is willing to help me I would really appreciate it.
Adherence =
var customerNames = VALUES(Customer[CUST_NAME_EXT])
var customerNumber = VALUES(Customer[CUSTOMER_EXT])
var selectedDates = VALUES('Fiscal Calendar'[fiscalDate])
var filterCustomerNames = FILTER(Customer,Customer[CUST_NAME_EXT] in (customerNames))
var filteDates = FILTER('Fiscal Calendar', 'Fiscal Calendar'[fiscalDate] in selectedDates)
var filterCustNumber = FILTER(Customer, Customer[CUSTOMER_EXT] in customerNumber)
var t = SUMMARIZECOLUMNS(Customer[CUST_NAME_EXT],Customer[CUSTOMER_EXT],'Fiscal Calendar'[fiscalYear],'Fiscal Calendar'[fiscalWeek], filterCustNumber ,"isAvailable", 1)
return t

First, I want to give you kudos for using SUMMARIZECOLUMNS. If I understand you correctly, you were trying to write DAX as if it is an SQL query, but it is not the case. To know the difference between them, you can read my answer to {How to write SQL in DAX?} question using this link:
How to write SQL in DAX?
Still, you can do it more easily by using the Power BI modeling strength because you said you had created a relationship using the date columns from both tables, Fiscal Calendar and Customer.
Second, to achieve your requirement, you need to create a flag to show 1 if a customer exists on a date or week and 0 if not, which, thankfully, can be implemented for any segregation level (columns) in your Fiscal Calendar table, just by two simple measures, which are as follows:
Count Rows = COUNTROWS(Customer)
Adherence = IF(ISBLANK([Count Rows]),0,1)
Finally, as your image shows, you will use the simple Adherence measure to give you the needed result.
In the end, I want to give you a helpful tip; I see you are trying to create a custom heat map using the Power BI matrix, which is very cool. Please use this link as a reference for more design ideas for what you are trying to achieve. The link is:
https://sqlskull.com/2020/12/17/implementing-a-table-heat-map-in-power-bi/#:~:text=A%20Heat%20map%20is%20used%20to%20visualize%20the,to%20apply%20some%20conditional%20formatting%20in%20matrix%20table.
I hope I helped in a way; if so, please mark this as an answer and vote for it :)

Related

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
)
)

Creating a Quick Calculation with Dates that Repeat in PowerBI

So, I'm in PowerBI Desktop. I have a table that pulls in data for various properties (website A, website B, website C) and creates a row for each property, for each day. So, for example, it'd look like this over the course of three days.
A snapshot of the data:
I need to create a single measure, showing the total number of returning users (for all properties) month-to-date.
My original plan was to do Quick Measures > Time Intelligence > Month-to-date Total.
The "Quick Measure" form I'm using:
This creates this measure:
usersReturning MTD =
IF(
ISFILTERED('dailyLog'[date]),
TOTALMTD(SUM('dailyLog'[usersReturning]), 'dailyLog'[date].[Date])
)
However, when I try to make this value shown in a card tile, it just shows (blank). And in the past, I know at some point it creates another kind of error. (I'm having difficulty replicating the value.) I'm wondering if this is because I don't have unique dates, but repeating dates? But I'm not getting any feedback on why.
I'm relatively new to PowerBI, and particularly to the quick measures and DAX scripting. So open to help or suggestions, and wondering if there is a way to make this work with the data schema that I'm showing here.
Based on this data...
You can use a TABLE visualization. Don't check date, & use sum on the returning users to get this report:
Your "single measure" is 7565, but you can also see a summary of A, B, & C to understand how that measure came to be. But if you really just want the 7565 all by itself, then select Card as the visual:

Sumif using page, report and visual level filters

I have two tables that are related by an ID column called 'Program_Code' (1:Many).
'Program_Summary':
Program Code = each row has a unique ID, e.g. HI-18, HI-17
Program Name = name of a program, e.g. Home Improvement
Incentive Spending = calculate(sum(Program_Data[Incentives]))
'Program_Data':
Program Code = many rows with the same ID
Incentives = incentive amount to summarize in Program Summary table
Record Status = Claimed, Pipeline or Rejected
Record Fiscal Year = 2017, 2018 or 2019
I created a Power BI table that has rows organized by 'Program Name'. Note that each program name like "Home Improvement" may have more than one code associated with it, e.g. HI-18 and HI-17 corresponding to fiscal years.
I'm hoping to summarize Incentive Spending by program name, and use page/report level filters to restrict results. The Report Level filters are:
Record Fiscal Year = 2017
Record Status = Claimed
But, the calculate(sum(Program_Data[Incentives])) filter ignores these page level filters. How do I fix this?
You created "Incentive Spending" as a calculated column. Instead, you need to create it as a measure.
Calculated columns are calculated only once - when you create them, or when you reload data. After that, calculated columns contain only static data, and can not respond to any filters.
Measures, on the other hand, are dynamic formulas that recalculate any time you change any filters.
To fix your problem, simply create a new measure from "Modeling" tab:
and add DAX code:
Incentive Spending = SUM(Program_Data[Incentives])
(no need to use CALCULATE here).
Drop this measure into a table or matrix, and it should work. Instead of page/report level filters, I'd recommend to use slicers - create a slicer for Fiscal Year, and another slicer for Record Status. They will allow you to filter the calculation with ease.
You can use:
CALCULATE(sum(Program_Data[Incentives]);Program_Data[Record Fiscal Year] = 2017 && Program_Data[Record Status] = "Claimed")
However, I do not understand why you would need this because you have 2 tables with the right link, this should give you all possibilities with the table/matrix visualization what you need to show correct results..

Creating a measure using DAX function Left on a table from Azure Analysis service

I am trying to get the first 4 digits from a string from a table in Power BI. The connection is a live connection / Direct which does not allow me to edit the query. Also, I am unable to create a new column. So I have to stick with creating a new Measure.
Now, I am using the following formula to get what I need.
LocationCd = mid(vw_DW_Contracts[ContractNumber],1,5)
but, this is not working a the vw_DW_Contracts table cannot be used in a measure. Is there a workaround to such problem?
I do not have access to the analysis service so cannot make any modifications in the source.
Please help.
Thanks
but, this is not working a the vw_DW_Contracts table cannot be used in a measure.
I'm not sure what you mean by this, but I'm guessing the message you see is telling you that measures expect an aggregation. The formula you posted would be great as a calculated column where it can be evaluated row by row. Measures are aggregations over multiple rows.
If you are trying to make a new field that is the location code that can be used in visuals on a categorical axis, this should be a column rather than a measure. You could write a measure to show a location cd using something like LASTNONBLANK (mid(vw_DW_Contracts[ContractNumber],1,5), 1) but I doubt that is what you want.

How to compare two columns values in the same table PowerBI DAX

I am working in a data analysis project using MS Power BI, thankfully I'm doing good work to start. However, I'm facing a little problem with DAX syntax. I come from a web development background. Anyways, my current problem is that I have rental vehicles, which can be rented from one branch and handed in at another.
I would like to compare two columns values in the same table. 'owner_branch' and 'current_branch'. Is it a good choice to create a filter with DAX? Or should I move to R Language?
If I understood your problem correct you need Calculated Column like this:
CompCol = IF ( Owner_Branch = Current_Branch, TRUE, FALSE )
as a temporary solution which I think that is not an efficient solution for a larger records in future. Anyways, my solution was creating a new column type of boolean.