## Issues with Data Sorting - powerbi

### How to display data for last few days but cumulate records from beginning of time in Tableau

I am building a Bar graph in Tableau to show daily cumulative record since January 2018. Due to number of days elapsed since then, the bars are so crunched that it looks ugly.
That said, how can I only display data for last few days while retaining the actual YTD count.
For e.g. starting January 1st I had 0 logins, February 1st - 100 YTD logins, March 1st - 200 YTD logins, April 1st - 300 YTD logins, and May 1st 400 YTD logins. I want the graph to display bars from May 1st only and show the starting count as 400 YTD logins and not zero.
PS: I am new to Tableau, so please assume that I don't know advanced stuff.

It seems that you are using Fit Width option which forces everything to be shown in your screen width. If so, try changing this to Standard, so that you may have control on the width of your bars and your users can see every month in one viz with a horizontal scrollbar.
If you do not want a scroolbar'd solution, you need to create fixed calculated fields to not get effected by the filters and use those new fields in your graph. At this point you may filter the date automatically (again with a calculated field based on the current date) or make your users manually filter what they want.
We may be in some more help if you can send a sample workbook but also you may play around with LOD expressions, FIXED keyword or simply get your calculations between { and } to fix them globally.

If you manually change the axis range, all table calculations, such as running totals, remain unaffected. You can do this by right clicking on the axis header, selecting "Edit Axis", then selecting the "Fixed" option under "Range", and choosing the dates you want to show.

### How do I create a “Users by time of day” heatmap in Google Data Studio?

I want to create a heatmap in Google Datastudio similar to the "Users by time of day" heatmap in Google Analytics.
This is how it looks in Google Analytics.
THE FIRST PROBLEM is that when I have my columns as the days of the week, it sorts on ascending/descending value alphabetically, NOT Sunday, Monday, Tuesday, etc.
THE SECOND PROBLEM is that I can't actually get the heatmap coloring.
THE THIRD (not a problem, but a preference)...is there a way to group every 2 hours like Google Analytics does?
I am creating this using the PIVOT TABLE chart. Should I be using a different chart? Any other ideas?

You can get something pretty close by doing this.
Pivot table
Row Dimension - Hour
Column Dimension - Day of week
Metric - Users
Sort Row 1- Hour - Ascending
Sort Column 1 - Day of Week Ascending.
Then under the Style tab
Metric - change from Number to Heatmap

Curious if there are any better answers for this. Seems like Data Studio is getting pushed more heavily now.

For the 2h interval I solved it by going in to the data source editor, click count on hour then it creates a copy that takes a formula, so i entered the formula
CASE
WHEN HOUR IN ("00","01") THEN "00-02"
...
...
...
WHEN HOUR IN ("22","23") THEN "22-00"
ELSE "Other"
END

### Calculate rates from previous month based on current month in order to make then visually comparable

Maybe this is an easy one but since I’m very new in DAX and PowerBI I can’t figure it out. My database has daily data ranging from MAY/17 to JUN/17 (and it’ll still going in the future). It has information of DATE, DAY, YRMTH (year-month), QT_APRV (approved customers) and QT_TOTAL (total consumers). Sample below (using Excel just to be quicker):
I wanted to create in PowerBI a bar chart with QT_TOTAL per day and a line chart with approved rate of consumer. For the rate, I used:
APPRV_RT = SUM(database[QT_APRV]/SUM(database[QT_TOTAL])
And then, selecting only a month by time in the chart (just like I want), I have:
Perfect, but now I want to create a new line chart, showing the approved rate in each respective day of the last month. Using my example, when june data are select, the first line chart has to show the daily approved rate of june AND the other the approved rate of may, in order to make it comparable (june1 -> may1; june12 -> may12 and so on). Here’s what I want:
How to make this automatically, in order to make each month comparable with the previous? I thought about some DAX formula involving a sum with filtering current month minus 1, I don’t know how to do it.
Any ideas?
UPDATE (07/08/2017)
I tried what Rasmus Dybkjær suggested me, and I thing I'm in the right path.
APPROVED_RATE_PREVIOUS_MONTH = CALCULATE([APPROVED_RATE_CURRENT_MONTH];PARALLELPERIOD(dCalendario[DataBase];-1;MONTH))
However, it returned the approved rate from the previous month as a whole (67,0% in May), not each day as I wanted:
Any suggestions?

You should be able to use the DAX function called PARALLELPERIOD() for this purpose. The PARALLELPERIOD() function takes a date column and shifts it a number of periods back or forward.
For example, you could use it like this to calculate your approved rate shifted one month back:
ApprovedRateLM =
CALCULATE(
DIVIDE(
SUM(database[QT_APRV]),
SUM(database[QT_TOTAL])
),
PARALLELPERIOD(database[Date],-1,month)
)
Note that it is important that your [Date] column contains a date type. Otherwise the PARALLELPERIOD function will not work.

### Dax Calculation with logic in power bi data analytics

Need help in Data Analytics Calculations.
Currently, I am getting historical data for consumption as follows:
on above data, I am adding custom columns for calculating exact consumption(gallons) in no. of days. like:
Now, I have to plot month wise bar chart for consumption of respective Meter ID in 2016 year. But problem here is, I will have to calculate Every months consumption by dividing it in days in each respective month of 2016, and then only I will able to plot them monthly like:
y axis = consumption in every month
x axis = Jan Feb March Apr May Jun Jul Aug Sep Oct Nov Dec
so, in jan month, consumption should be = 10 + 100 + ((115/38) * 7) gallons
Notes: here, in ((115/38) * 7) : we are calculating avg consumption of single day 7 days in Jan and whole march and then getting last 7 day consumption of Jan so that we can add it in calculation of total consumption of Jan month
but how to add measure/custom column/new table for these calcualtions?
Thanks

What you need to do is relatively complicated, but the summary of my solution is:
Calculate the per-day consumption
Calculate the start and end date of each reading (e.g. the previous reading date plus one day, and the reading date)
Expand your data to have 1 row per day rather than 1-row per reading
You want to do these steps before you load the data into your data model (i.e. in your source system, or as the data is loaded using the Query Editor/Power Query).
Below, I assume you're using the Query Editor/Power Query. However, if you can use your source system, it's often the better choice (since the source system may be a database that is vastly faster than your desktop).
Note that your No. of Days calculation doesn't make sense to me. There are more than 38 days between 24 Jan 2016 and 31 Mar 2016. There are also more than 13 days between 10 Jan and 24 Jan. For this reason, it was difficult to tell whether you wanted a new reading to count on the day the previous reading was taken, or on the next full day. I assume the former. Also note, I've proceeded on the basis that your No. of Days calculation is correct
Calculate the Per Day Consumption
This is the easiest step, given that you have already calculated the Consumption and the No. of Days. Just divide one by the other. In the Query Editor, you can click in the Consumption (gallons) column and select Add Column > Standard > Divide. Under Value, choose Use values in a column and then select the No. of Days column.
Calculate the Start & End Date of Each Reading
The date of the reading is the end date, so you can rename Date to be End Date (since a reading is applied retroactively).
For the start date, in the Query Editor, you will need to add an index column (Add Column > Index Column). You will want to make sure your data is sorted by Meter ID and Date Ascending before doing this. Call the column Index.
Next, Add Column > Custom Column and pull the reading date from the prior row. Call the new column Previous End Date for now.
// A try is necessary because we can't get the previous row if there is no previous row (we'll get an error, which we can handle in the 'otherwise' block)
try
if
// See if the previous row is for the same Meter ID
[Meter ID] = #"Reordered Columns"{[Index] - 1}[Meter ID]
then
// If it is, grab the Reading Date from the previous row
#"Reordered Columns"{[Index]-1}[End Date]
else
// If this is the first reading for a meter, calculate the Start Date by subtracting the No. of Days from the End Date
otherwise
// If this is the first row in the table, also calculate the Start Date by subtracting the No. of Days from the End Date
Next, you'll want to add 1 to the Start Date, as we want the reading to apply to the day after the previous reading, not on the day of the previous reading.
Note, if you want the reading date to count in the prior period, subtract 1 from the End Date rather than add 1 to the start date (previous end date).
Expand your data to have 1 row per day
At this point, you should have a Meter ID, Start Date, End Date, and per day consumption column that reflects what you expect (i.e. the per day consumption is correct for the date range).
The final step is to duplicate each row for each date in the date range. There are several solutions to this outlined in this thread (https://community.powerbi.com/t5/Desktop/Convert-date-ranges-into-list-of-dates/td-p/129418), but personally, I recommend the technique (and video) posted by MarcelBeug (https://youtu.be/QSXzhb-EwHM).
You should end up with something more like this (after some removing & renaming of columns):
Finally
Now that you have one row per meter & date, with a per day consumption already calculated, you can build a visual. For example, you could do a column chart with Date on the Axis, and Consumption per Day as the value. By default, Power BI will recognize that Date is a date, and will roll it up by Year-Quarter-Month-Day. Press the little 'x' by Year and Quarter, and you'll have a chart that sums up the per day consumption by month. You can also drill down to individual date.
Reading a value from a previous row in Power Query
If Statements in Power Query
The AddDays function in Power Query
Catching Errors in Power Query
Converting a date range into a list of dates (Marcel Beug's solution)
A similar problem I previously answered

### Working with imperfectly matched data in crossfilter

Given the following dimensions specifications in Crossfilter's API - https://github.com/square/crossfilter/wiki/API-Reference
1.) The function must return naturally-ordered values
2.) .....incomparable values such as NaN and undefined are not supported
How would one go about charting a crossfilter (using dc.js) with two dimensions - one with daily data (7 days a week), and another with business-day data (5-days a week)? The data structure implies that the business-day data will have gaps on the weekend which should violate the specifications above.
For example, if I want to compare a company's store sales (7 days/week) vs its stock price (5 days + gaps on Saturday and Sunday), how would i go about it? The goal would be to have two dc.js charts filtering each other, but having data that isn't perfectly matched up i.e. the first chart will show sales data from Jan 1 till Jan 31 (7 days a week), while the second chart will show stock price data from the first till the last business day in Jan (excluding weekends).

Your stock data would likely include no data for Saturday and Sunday. This is is different from having a data row with stock price as NaN.
For example: If you plotted the stock data on a row chart with the days of the weeks for the categories, then there would be no bars for saturdays and sundays.
Here is a crude example: DC.JS example of days of week chart
I made sure that no rows were added for saturdays and sundays:
if ((stockDate.getDay() != 6) && (stockDate.getDay() != 0))
The resulting row chart has no row for Saturday or Sunday.

You could explore filtering your data, as I did, so you preselect what you want to show. Remember to include the additional code which preserves the bins.
Hide Specified Row in dc.js rowchart