I have a SQL query that creates a table, and every month 2 new columns will be added for that table related to the current month.
I have tried without success to set up a flat table (visual) in Qlik that will automatically expand every month to include these table. Is there a way to do this, and i so please point me in the right direction.
You can have a look at CrossTable prefix.
This prefix allows a wide table to be converted to a long table.
So if we have data like this:
After running the following script:
CrossTable:
CrossTable(Month, Sales)
LOAD Item,
[2022-10],
[2022-11],
[2022-12],
[2023-01],
[2023-02],
[2023-03],
[2023-04]
FROM
[C:\Users\User1\Documents\SO_75447715.xlsx]
(ooxml, embedded labels, table is Sheet1);
The final data will looks like below. As you can see there are only 3 columns. All xls month columns (after Item) are now collapsed under one field - Month and all the values are collapsed under Sales column.
Having the data in this format then allows creating "normal" charts with adding Month column as dimension and use sum(Sales) as an expression.
P.S. If you dont want to manage the new columns being added then the script can be:
CrossTable(Month, Sales)
LOAD
Item,
*
FROM
...
Related
I have data from two tables modelled as follows:
my desired result is:
I've tried SUM(table1[balance],ALLEXCEPT(table1,table1[account])) - doesn't work
this may be simple, but PowerBi is evading me at this point...
Pretty simple to solve it by with extra steps to complete it:
First, on your second table (I named it "cust"), add a new column with the balance from your first table (I named it "acc"):
Balance = LOOKUPVALUE(acc[balance],acc[account ],cust[account])
Cust
Next, I will add a new column to lookupvalue on my "acc" table, follow by adding new column to return the sum value from my "cust" table:
sum = CALCULATE(SUM(cust[Balance]),FILTER(cust,cust[customer ]=EARLIER(acc[customer])))
Acc
I have the following view set up in SQL Server:
VIEW
(left table: population data per year; middle table: municipalities; right table: municipality areas in km²)
Query
SELECT
dbo.T_GEMEINDE.GKZ, dbo.T_GEMEINDE.NAME,
dbo.T_BASE_DAUERSIEDLUNGSRAUM_GEMEINDE.FLAECHE_KM2 / dbo.T_BASE_DAUERSIEDLUNGSRAUM_GEMEINDE.DAUERSIEDLUNGSRAUM_KM2 AS [ges. Fläche / Dauersiedlungsr.],
dbo.T_BASE_GEMEINDE_BEVOELKERUNG_JAHR_BEGINN.J2017 / dbo.T_BASE_DAUERSIEDLUNGSRAUM_GEMEINDE.FLAECHE_KM2 AS [ges. Bevölkerungsdichte],
dbo.T_BASE_GEMEINDE_BEVOELKERUNG_JAHR_BEGINN.J2017 / dbo.T_BASE_DAUERSIEDLUNGSRAUM_GEMEINDE.DAUERSIEDLUNGSRAUM_KM2 AS [Bevölkerungsdichte Dauersiedlungsraum]
FROM
dbo.T_BASE_DAUERSIEDLUNGSRAUM_GEMEINDE
INNER JOIN
dbo.T_GEMEINDE ON dbo.T_BASE_DAUERSIEDLUNGSRAUM_GEMEINDE.GKZ = dbo.T_GEMEINDE.GKZ
INNER JOIN
dbo.T_BASE_GEMEINDE_BEVOELKERUNG_JAHR_BEGINN ON dbo.T_GEMEINDE.GKZ = dbo.T_BASE_GEMEINDE_BEVOELKERUNG_JAHR_BEGINN.GKZ
The last column in the view contains a calculation (population density for 132 municipalities for a certain year) for the year 2017 and uses the column J2017 from the table seen on the left. This is the output (Bevölkerungsdichte Dauersiedlungsraum):
Current output:
OUTPUT
Desired output:
The rightmost column (Bevölkerungsdichte Dauersiedlungsraum) seen in the provided output screenshot has the output data of the calculation for the year 2017. The same output has to be generated for all the other years, but each as a separate column.
Question: how do I perform the calculation which you can see in the last column in the view for all years (J2017-J2050) without having to do it manually for each year column?
Thanks in advance.
if you want someone to provide you with a complete solution then you will need to supply:
CREATE TABLE statements for the 3 tables
INSERT INTO... statements to provide sample data for all 3 tables
However, if you just want a suggestion about how to approach this problem then I would use an UNPIVOT statement to create a view/table that
holds all the columns in dbo.T_BASE_GEMEINDE_BEVOELKERUNG_JAHR_BEGINN
apart from the "year" columns (J2017, J2018, j2019, ...)
adds a single "year" column with values from 2017 to 2050
adds a single value column to hold the population for each year
By joining your existing tables to this new table/view and grouping by your new "year" column you should achieve what you want
How to write this expression in PowerBI
select distinct([date]),Temperature from Device47A8F where Temperature>25
Totally new to PowerBI. Is there any tool that can change the query from sql to PowerBI expression?
I have tried so many type of different type of expressions but getting error, Most of the time I am getting this:
The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
Need help, Thanks.
After I posted my answer, wondered if your expected result is get only one date by temperature, In other words, without repeated dates in your result set.
A side note: select distinct([date]),Temperature from Device47A8F where Temperature>25 returns repeated dates since DISTINCT keyword evaluate distinct columns values specified in the SELECT statement, it doesn't return distinct values in a specific column even if you surround it with parenthesis.
Now what brings us here. What I can see in your error is that you are trying to use a table-valued (produces a table with multiple columns) expression in a measure which only accepts scalar-valued (calculate only one value).
Supposing you have a table like this:
Running your SQL query you will get the highlighted in yellow rows:
You can see 01/09/2016 date is repeated. If you want to create a measure you have to define what calculation you want to show for temperature. i.e, average, max or min etc.
In the below expression is being calculated the maximum temperature greater than 25 per date:
MaxTempGreaterThan25 =
CALCULATE ( MAX ( Device47A8F[Temperature] ), Device47A8F[Temperature] > 25 )
In this case the measure MaxTempGreaterThan25 is calculated per date.
If you don't want to produce a measure but a table. In the Power BI Tool bar select Modeling tab and click New Table icon.
Use this expression:
MyTemperatureTable =
FILTER ( Device47A8F, Device47A8F[Temperature] > 25 )
It should produce a new table named MyTemperatureTable like this:
I recommend you learn some basics about DAX, it is pretty different from SQL / T-SQL and there are things you can't do depending on your model and data.
Let me know if this helps.
You probably don't need to write any code if your objective is to show the result in a Power BI visual e.g. a table. Power BI naturally aggregates data if the datatype is numeric (e.g. Temperature).
I would just add a Table visual on a Report page and add the Date and Temperature columns to it. Then in Visualizations / Fields / Values I would click the little down-arrow on the Temperature field and set the Aggregation e.g. Maximum. Then in Visualizations / Fields / Filters I would click the little down-arrow on the Temperature field and set the Filter e.g. is greater than: 25
Hard-coded solutions are unlikely to survive the next question from your users e.g. "but what if I want to see Temperature > 24? Or 20? Or 30?"
For example I want to filter my data based on next filter expression:
lead_veh_of_interest starts with 'BMW 1'
OR
sale_model starts with 'BMW 1'
how can I achieve this?
Are these fields both in the same table? If so you could create a calculated column that performs that conditional. Then filter the view on the resulting calculated column.
For example you could create the following calculated column.
Calculated Column: "Starts With BMW 1"
Equation:
=IF(OR(Left([lead_veh_of_interest starts], 5) = "BMW 1", Left([sale_Model], 5) = "BMW 1"), 1, 0)
Then in the view set the filter so that [Starts With BMW 1] = 1
I found acceptable solution (thanks for all suggestions - it was very helpful).
First of all I redesigned my model and (how #Mike_Honey suggsts me) created dedicated table with consolidated information I want to filter. Next I connected this new table with existing tables and created hierarchy from fields I want to give to the end users for step-wise filtering (previously I split down old fields contained information I want to filter into more granular level). Now it is possible to filter data by any combination of models in any combination of request types (sale, lead, competitor, etc) using hierarchy.
I have an ssrs matrix , the design of which looks like this :
The sql query used in the above dataset looks like this :
select [YEAR], [MONTH] as MONT , ProductName, NumberofSales from XYZ ;
When the report is run , My output looks like this :
Here , the Column Names 9 , 10 correspond to the months 9 - September and 10 - October.
Change shows the difference in numbers between the month and the previous month . example :
number of sales in october - number of sales in september.
I would like to remove the column that I have circled in the above picture . Could you please let me know how I can do this . ??
I already tried right clicking the Change column and changing the visibility property, by adding an expression to look soomething like this :
=(Parameters!UserSelectedDate.Value=Fields!MONT.Value)
But that gives me an entire blank space for the Change column like this :
But I do, not want to see the blank space. I would like to remove the column completely.
Please let me know if it is possible.
I don't know, why it shows blank space, but here is how I handled this problem:
Choose "column visibility" property of column "Change"
Then I added expression for hiding column: =IIF(Month(Fields!Date.Value)=Parameters!UserSelectedDate.Value,True,False)
And when I select report parameter to 1 and preview the report, it hides Change column for month 1:
This answer works only for matrixed (varied count) columns.
But in all reports client want to group report data dynamicly and select column that one want to see.
So if I have matrix for example with columns -
[Store] [Good] [Amount Jan] [Amount Feb].... [Amount Dec]
I can hide any column with Amount but If I right click on column Store - No option in menu- Column properties and option Column Visibility - Disabled. If apply visibility to cells - you'll see a blank space.
I've looked for an answer for this issue and was frustrated that I could not find any. Then I found a workaround for this issue, I decided to share this.
You have to create column groups for all columns, which you would like to Show/Hide. For this matter you have to take or create one or more attributes in you ResultSet, which contain a constant value, so that the columns will not be later propagated.
After you created a new Column Group (as Adjacent Before/After)
you can put there the columns, which you like to Hide/Show.
After that you can use the property "Column visibility". It works just fine for me.