DAX for Grouping - powerpivot

I have following data set as data Model.
Country City AssetCount
USA Newyork 50
USA Washington 40
USA California 30
India Bangalore 100
India Delhi 50
India Bombay 30
I want to show one row showing sum of Assetcount at country level & city level on the same row.
There are two slicers for slicing City & Country as below:
USA Newyork
India Washington
California
Bangalore
Delhi
Bombay
So when I select country as India it should show sum of Asset-Count at country(India) level.
In the same way when I select City as Delhi it should show Asset-Count at City(Delhi) level.
India Delhi
180 50
Is it possible using PowerPivot using DAX?
Related content from their question on MSDN
Actually your solution is not working. I have created the hierarchy as Country-->City & kept that in Rows. So when I select a particular Country & City it showing like this:
Row Labels AssetCount
USA 40
Washington 40
Grand Total 40
But I want
USA Washington
120 40
or may be like
USA 120
Washington 40
I have tried some aggregate functions like below:
=SUMX(VALUES(Query[City]),CALCULATE(SUM(Query[AssetCount])))
=CALCULATE(SUM(Query[AssetCount]),SUMMARIZE('Query',Query[City]))
Here Query is table for Data Model & City can be replaced by Country.
but not working.
So showing such counts on same row is possible or not?

Sounds like you are just getting started with Power Pivot. You might browse through the links on this page for more help.
I took the data you provided and pasted it into Excel.
Selected the data and clicked Add to Data Model and checked the box for My Data Has Headers.
I made sure the AssetCount Column had a data type of whole number. Then clicked the Pivot Table button and created a pivot table on my existing spreadsheet.
I put AssetCount in the values and made sure it was set to Sum in the Field Value Settings.
I selected my pivot table and then went to the Analyze tab under PivotTable Tools and clicked the Insert Slicer button.
I selected both Country and City as slicers.
This gives your desired result.

If you want the two numbers in a row, that's pretty straightforward. Keep in mind, that all those slicers do is putting filters on the pivot table.
Therefore to get your city result, you could use either an implicit measure or explicit measure that simply sums up AssetCount.
For the country result, you'd wish to overload the city filter like this:
=calculate(SUM(Query[AssetCount]),ALL(Query[City]))
If you also need the country and city names there, it gets a bit tricky.

Related

How to pivot the table containing each value in the output row in SQL

I can't resolve this issue. I tried to use PIVOT() function, I've read the documentation and tried to use that. Additionally, I tried to find the answer but didn't find.
The main problem is using PIVOT() function, that it has to include aggregation function, but I needn't it, I need only pivot the table without any aggregation.
The source table:
COUNTRY
LEVEL
NUMBER
Germany
High
22
Germany
Medium
5
Germany
Low
3
Italy
High
43
Italy
Medium
21
Italy
Low
8
Canada
High
9
Canada
Medium
3
Canada
Low
13
I'd like to get the output table looks like:
COUNTRY
High
Medium
Low
Germany
22
5
3
Italy
43
21
8
Canada
9
3
13
Can anybody help me?
How to do that without using aggregation function or using but the get all values. Cause, for example, if I use min() or max() I get the max and min value and the others cells would be empty.
why do you think that using min/max would leave empty cells? As there is only one value for each country/level combination then using min or max is effectively just picking that one value.
Obviously, if your source data had more than one record for each combination of country/level then you'd need to decide how to deal with it.
This SQL seems to work fine:
select *
from COUNTRY_INFO
pivot(max(NUMBER) for LEVEL in ('High', 'Medium', 'Low'))
as p
order by country;

Pivot Table In BigQuery Using Standard SQL

I have a set of data (referenced the attached Excel sheet) that is unpivoted, which I am trying to pivot and convert the rows to columns.
In this case, the yellow headers are duplicated data. The green headers are what I want to convert from rows to columns, which will remove the duplication and thus pivot the data.
The top of the Excel sheet is what I have currently, the bottom is what I am wanting to get to.
I am using BigQuery standard SQL.
Any help is appreciated.
Excel Screenshot
Here is a short sample of what I have.
catalog_number
manufacturer
region
region_price
region_catalog_number
123
nike
north
10
123-n
123
nike
south
8
123-s
Here is a sample of what I want.
catalog_number
manufacturer
region_north
region_north_price
region_north_catalog_number
region_south
region_south_price
region_south_catalog_number
123
nike
north
10
123-n
south
8
123-s
Consider below simple approach
select *
from your_table
pivot (
any_value(region) region,
any_value(region_price) region_price,
any_value(region_catalog_number) region_catalog_number
for region in ('north', 'south')
)
if applied to sample data in your question - output is

Creating a map with sales data in SSRS, how do I connect my sales numbers to counties via county names?

My dataset looks something like this:
County Product Sales
CountyA ProductA 200
CountyA ProductB 150
CountyB ProductA 100
CountyB ProductB 300
CountyC Product A 50
etc...
I am using SSRS within Visual Studio. I am creating the map using the "Report Items" section of the toolbox.
I have the map with my state's counties, I associated the COUNTYNAME field with the County column in my data, but that's as far as I can get. I cannot seem to get the sales numbers to show on the map at all.
I can get the county names to show up on the map as labels, but that's not particularly helpful. If I choose [SUM(Sales)] nothing appears on the report.
Ultimately, I want a map of Product A sales vs Product B sales by county.
Is my dataset insufficient? And if so, what additional data do I need to add? Are there any steps I am missing to get the county associated with the sales column?

statistic syntax in access

I want to do some statistic for the Point in my appliation,this is the columns for Point table:
id type city
1 food NewYork
2 food Washington
3 sport NewYork
4 food .....
Each point belongs to a certain type and located at the certain city.
Now I want to caculate the numbers of points in different city for each type.
For example, there are two types here :food and sport.
Then I want to know:
how many points of `food` and `sport` at NewYork
how many points of `food` and `sport` at Washington
how many points of `food` and `sport` at Chicago
......
I have tried this:
select type,count(*) as num from point group by type ;
But I can not group the by the city.
How to make it?
Update
id type city
1 food NewYork
2 sport NewYork
3 food Chicago
4 food San
And I want to get something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0
I will use the html table and chart to display these datas.
So I need to do the counting, I can use something like this:
select count(*) from point where type='food' and city ='San'
select count(*) from point where type='food' and city ='NewYork'
....
However I think this is a bad idea,so I wonder if I can use the sql to do the counting.
BTW,for these table data,how do people organization their structure using json?
this's what you want:
SELECT city,
COUNT(CASE WHEN [type] = 'food' THEN 1 END) AS FoodCount,
COUNT(CASE WHEN [type] = 'sport' THEN 1 END) AS SportCount
FROM point
GROUP BY city
UPDATE:
To get the results in an aggregated row/column format you need to use a pivot table. In Access it's called a Crosstab query. You can use the Crosstab query wizard to generate the query via a nice UI or cut straight to the SQL:
TRANSFORM COUNT(id) AS CountOfId
SELECT type
FROM point
GROUP BY type
PIVOT city
The grouping is used to count the number of Id's for each type. The additional PIVOT clause groups the data by city and displays each grouping in a separate column. The end result looks something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0

Crystal reports - missing fields

using Crystal reports 10 linked to an excel document. Would like to pull the dinner field but also pull country and Company name from row that dont have it, this are linked via Bookingref. Example below. I've tried sub-reports and supressing unwanted fields but can't get it right. Also I can't make changes in excel doc as it's 1000+ records, which is exported from an online system weekly.
Id BookingRef Country CompanyName Surname Forname Dinner
1 001 UK Company1 John Andrews
2 001 Mary Jane 1
3 001 Tom Andrews 1
4 002 Germany Company2 Lee Jones
5 003 Germany Company3 Peter Lee 1
6 003 Sofie Lee 1
OK I am not sure I understand the full extent of your problem but let's start with the Country and Company name and see if I can get you moving forward. Instead of putting the Country field directly on the report you could use a formula field and do something like this:
IF {#BookingRef} = "001" Then
"UK"
Else IF {#BookingRef} = "002" Then
"Germany"
Else
"Unnamed"
Now you just put the formula field where the country field used to be and it will put the right country in bases on the BookingRef code. This, however, is only practical if you are working with a small number of Country / Company Names or possibly a big list that never changes although I would caution against the latter.
The other thing you could do is create a table in any database that holds the BookingRef, Company and Country values, link the BookingRef fields from both "databases" and then just drop the fields on your report.
If I am missing the point of your question please be real specific about what it is you are trying to accomplish and what is and is not working in your current solution.