Create chart where the columns of the data set are the categories - Report Builder 3.0 - sql

I have a very simple chart that I am wanting to add but I can't for the life of me figure it out. The chart is referencing a dataset that returns data like this. It is calculating the sum of each Location and then using Rollup to produce a Total Count for each Week Column
Location CurrentWeek PreviousWeek 2WeeksAgo
======== =========== =========== ===========
North 5 6 3
South 4 3 1
East 8 2 3
West 2 7 0
Total 19 18 7
What I am wanting to do is have the X Axis (horizontal) represented by the CurrentWeek, PreviousWeek, 2WeeksAgo columns and plot the "Total" values from each respective column.
Adding Snip...
Sample Chart

Thanks for adding the image.
So we have a few steps to get to where we need to be - first, we need to transform the data into a format that's easier and more scalable to work with (if we ever add a "3 weeks ago" column, we don't want to have to rework everything). The desired format is:
Date Amount
Current Week 19
1 week ago 18
2 weeks ago 17
Personally - instead of naming stuff "current week", "1 week ago" etc., I would have a WeeksPrior column where 0 would mean the current week, 1 would mean a week ago and so on.
Anyways, to get from your sample table to the more standardized input, we have to use an unpivot (these always hurt my brain, but the docs have some good examples you can use).
SELECT
*
, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Ordinal
--this is hacky, but ordering by (select null) allows us to assign a row number by the default order
FROM (SELECT 'Total' AS Location, 19 AS CurrentWeek, 18 AS PreviousWeek, 7 AS [2WeeksAgo]) x
--This is the test data, replace this with your actual query
UNPIVOT (Value FOR Date IN ([CurrentWeek], [PreviousWeek], [2WeeksAgo])) y
--This unpivots the test data, converting the separate columns into a single [Date] column, and assigning the values to the [Value] column.
This will spit out the following:
Location Value Date Ordinal
Total 19 CurrentWeek 1
Total 18 PreviousWeek 2
Total 7 2WeeksAgo 3
From here, we add the data to the chart. This is pretty straighforward, but there are a few "gotchas" to be wary of.
First, we'll add the Value column as a chart value, and the Ordinal column as a category group.
Let's see what the chart looks like right now by running the report.
Well, it's getting there, but we want our labels on the bottom. To do this, we go into the Ordinal category group's properties and switch the label to the date column. Make sure you're still sorting by Ordinal, since SSRS doesn't know what "1 week ago" means relative to "Current Week", and will sort alphabetically or randomly if you don't tell it to sort by ordinal.
We can also clean up the chart a bit by removing the legend and changing the major tick mark line style to "solid" on the horizontal axis., leaving us something that looks like this:
Adding a label to the vertical axis would probably also help readability, as would adding hover-text to the points on the chart.

Related

Need column comprised of data from date two weeks ago for comparison

Let me start by saying that I am somewhat new to SQL/Snowflake and have been putting together queries for roughly 2 months. Some of my query language may not be ideal and I fully understand if there's a better, more efficient way to execute this query. Any and all input is appreciated. Also, this particular query is being developed in Snowflake.
My current query is pulling customer volumes by department and date based on a 45 day window with a 24 day lookback from current date and a 21 day look forward based on scheduled appointments. Each date is grouped based on where it falls within that 45 day window: current week (today through next 7 days), Week 1 (forward-looking days 8-14), and Week 2 (forward-looking days 15-21). I have been working to try and build out a comparison column that, for any date that lands within either the Week 1 or Week 2 group, will pull in prior period volumes from either 14 days prior (Week 1) or 21 days prior (Week 2) but am getting nowhere. Is there a best-practice for this type of column? Generic example of the current output is attached. Please note that the 'Prior Wk' column in the sample output was manually populated in an effort to illustrate the way this column should ideally work.
I have tried several different iterations of count(case...) similar to that listed below; however, the 'Prior Wk' column returns the count of encounters/scheduled encounters for the same day rather than those that occurred 14 or 21 days ago.
Count(Case When datediff(dd,SCHED_DTTM,getdate())
between -21 and -7 then 1 else null end
) as "Prior Wk"
I've tried to use an IFF statement as shown below, but no values return.
(IFF(ENCOUNTER_DATE > dateadd(dd,8,getdate()),
count(case when ENC_STATUS in (“Phone”,”InPerson”) AND
datediff(dd,ENCOUNTER_Date,getdate()) between 7 and 14 then 1
else null end), '0')
) as "Prior Wk"
Also have attempted creating and using a temporary table (example included) but have not managed to successfully pull information from the temp table that didn't completely disrupt my encounter/scheduled counts. Please note for this approach I've only focused on the 14 day group and have not begun to look at the 21 day/Week 2 group. My attempt to use the temp table to resolve the problem centered around the following clause (temp table alias: "Date1"):
CASE when AHS.GL_Number = "DATEVISIT1"."GL_NUMBER" AND
datevisit1.lookback14 = dateadd(dd,14,PE.CONTACT_Date)
then "DATEVISIT1"."ENC_Count"
else null end
as "Prior Wk"*
I am extremely appreciative of any insight on the current best practices around pulling prior period data into a column alongside current period data. Any misuse of terminology on my part is not deliberate.
I'm struggling to understand your requirement but it sounds like you need to use window functions https://docs.snowflake.com/en/sql-reference/functions-analytic.html, in this case likely a SUM window function. The LAG window function, https://docs.snowflake.com/en/sql-reference/functions/lag.html, might also be of some help

Calculate average VBA from dynamic table

I ve a table on Excel with the week number and the weight (in Kg.)
Somebody can insert his weight every day during a week or just once or not at all. I can't manage that.
Then my table can literally change. from zero to 7 even more lines a week (like the yellow side of the image).
What I wanna do is to calculate the weight average per week. and then I will have one line for each week, when i got at least one weight (sometimes I won't have any line). We can have week without any weight so then I don't want this line at all. We can also easily have a weight for the week 2 but between the weeks 5 and 6 in the yellow table. That would happen if someone insert his weight after others.
How can I say this two weeks are similar, so we calculate the average for this two weight ?
I hope it's enough clear with this picture
Use formula below in Column C to calculate average(assume Week in column A and Weight in column B)
=AVERAGEIF(A:A,A2,B:B)
Average Column Copy->PasteSpecial value only,
then Remove Duplicates base on Week and the new Average Column

How to show last 13 months in X axis based on single slicer selection?

EDIT: As this is an old question, the intended solution is supported by the relative date slicer in Power BI as seen here.
I am building a rolling 13 month net revenue chart and I've came across a requirement to show the last 13 month in x axis based in what month (YYYYMM) the user selected in a YYYYMM slicer.
The chart below is the one I want:
In order to get the intended behaviour, currently I need to select each one of the 13 month in YYYYMM slicer:
Any ideas on how I can get this to work in Power BI so that the user just have to select the base month?
Thanks in advance,
Cristhian.
Let's say you have a table data(date, value).
Add a table datefilter(date) with dates you want to select. Add a slicer by this date.
Add a calculated column to data : date2 = DATEADD('data'[date].[Date],13,MONTH)
Add this measure:
current = IF(AND(
MAX('data'[date]) < MAX('datefilter'[date]),
MAX('data'[date2]) > MAX('datefilter'[date])),1,0)
filter your visualisation by current is 1.

Set ascendnig X axis in a QlikView chart

I have a qlikview chart where the dimension is the week of a date.
So i get a groupped sum of the values for weeks.
Its good but when its start from the middle of the year it will start ascending e.g.:
y->2 5 2
x->22 23 24 etc
but i want:
y->2 5 2
x->1 2 3
But I tried to change dimension somehow but all faild, I tried at the sort tab some expression or the layout but i simply cant find to to replace the x-axis values.
So now the dimension (x) is Calender_Week of the date. but it returns 0-52 the week of the year.
But I dont want to show this 0-52 I want a static numbers.
If week 26-32 then 1-2-3-4-5-6-7
If week 12-13 then 1-2
If week 51-52-01-02 then 1-2-3-4
The reason that you cannot sort the values correctly is because the week number is just a value between 1 and 53. You cannot specify a sort order since there is no way of telling which year the week number belongs to.
However, there is a way to resolve this. In the following example, let's assume that I have my data table MyData defined as follows:
MyData:
LOAD
*
INLINE [
CalendarDate, Result
15/12/2014, 5
16/12/2014, 6
22/12/2014, 10
29/12/2014, 20
30/12/2014, 30
05/01/2015, 20
];
I have another table which loads from MyData and derives the week number:
MyChartData:
NOCONCATENATE
LOAD
WeekNumber,
Sum(Result) as Result
GROUP BY WeekNumber;
LOAD
Week(date#(CalendarDate,'DD/MM/YYYY')) as WeekNumber,
Result
RESIDENT MyData;
DROP TABLE MyData;
If I chart these data I have the problem of sorting (at first glance it seems okay, but in this instance week 1 should come after week 52):
Now, to solve this we need another way to sort the data but still keep it grouped at a weekly level. As I have the original calendar date, I can create a new field which does just that: a concatenation of week number and year (i.e. YYYYWW):
NOCONCATENATE
LOAD
WeekNumber,
YearWeek,
Sum(Result) as Result
GROUP BY WeekNumber, YearWeek;
LOAD
WeekNumber,
(WeekYear * 100 + WeekNumber) as YearWeek,
Result;
LOAD
Week(date#(CalendarDate,'DD/MM/YYYY')) as WeekNumber,
WeekYear(date#(CalendarDate,'DD/MM/YYYY')) as WeekYear,
Result
RESIDENT MyData;
If I then swap out WeekNumber from my chart and replace it with WeekYear then things are looking better:
Edit - now to relabel the dimension values
To relabel the YearWeek values so that they are just shown as an relative index (or "column" number), we can use a calculated dimension and utilise the aggr function as follows:
=aggr(RowNo(), YearWeek)
This then results in a chart as follows:
The advantage to this method is that whichever selections you make in your QlikView document, the relative index will remain contiguous even though the underlying data could be discontinuous. Furthermore this does not hard-code the index into the data model.

SQL change over time query

I have created 2 tables. one table has 4 fields. a unique name, a date and 3 figures. The second table contains the same fields but records the output of a merge function. therefore has a date at which time the update or insert function happened. what I want to do is retrieve a sum of either the difference between 2 days or alternatively the totals of the 2 days to work out how much the value has changed over the day. The merge function only updates if a value has changed or it needs to insert a new value.
so far I have this
select sum(Change_Table_1.Disk_Space) as total,
Change_Table_1.Date_Updated
from VM_Info
left join Change_Table_1
on VM_Info.VM_Unique = Change_Table_1.VM_Unique
where VM_Info.Agency = 'test'
group by Change_Table_1.Date_Updated
but this would just return the sum of that days updated total rather than the difference between the two days. One answer to this question would be to to add all new records to the table but this would contain a number of duplicates. So in my head what I want it to do is loop over the current figures for the day then loop over the next day but also to include all values that haven't updated. sorry if I haven't explained this well. so what I want to achieve is to get some sort of change of the total over time. If its poor design im in a position to accept that also.
Any help is much appreciated.
maybe this would explain it better. show me total for day 1, if the value hasn't changed then show me the same value for day 2 if it has changed show me new value. and so on...
ok to further elaborate.
the Change_Table looks like
vm date created action value_Field1 value_field_2 Disk_Space
abc 14/10/2013 insert 5 5 30
def 14/10/2013 insert 5 5 75
abc 15/10/2013 update 5 5 75
so the out put I want is for the 14th the total for the last column is 105. On the 15th abc has changed from 30 to 75 but def hasn't changed but still neds to be included giving 150
so the output would look like
date disk_Space
14/10/2013 105
15/10/2013 150
Does this help? If not, can you provide a few rows of sample data, and an example of the desired result?
select
(VM_Info.Disk_Space - Change_Table_1.Disk_Space) as DiskSpaceChange,
Change_Table_1.Date_Updated
from
VM_Info
left join Change_Table_1 on VM_Info.VM_Unique = Change_Table_1.VM_Unique and VM_Info.Date = Change_Table_1.Date_Updated
where
VM_Info.Agency = 'test'