How to change a attribute to a measure with DAX? - excel

I have a lot of data in my database and I wish to display it as a Pivot Table using Power Pivot, however, some of my data is text (some flags, month names, names, last names, etc). Is there a way that I could convert these attributes to measure so the overall peformance of my Pivot table doesn't get affected?
I have some ideas but nothing clear yet, here is a sample of my table
UserID | Name | LName | City | State | QtySold
1111 Jon Smith 2 6 459394
1112 Alex Cash 6 2 31232
1113 Rob Adams 5 1 12434
I only need to use these attributes when I'm looking the data at UserID level (if I see the data at City, State leven It won't mather since I don't care about names / last names) is there a way I could evaluate the context and make the Name a measure?
Something like
=IF(UserID, DisplayLastName, BLANK())
(A new measure that checks if I'm using UserID, If I'm then display the LastName, If not, display blank)
Any ideas?
Thanks,

Oh well it seems that I found what I was looking for
LName:=IF(ISFILTERED(Details[UserID]),VALUES(Details[LName]),BLANK())
As long as UserID is on the Pivot Table, the LName measure will work.

Related

Pivot table showing too many rows with DAX measure

Consider the following two tables
Major
Major Department
Department Name
Department Number
Produce
Produce
2
Produce
Taxable Produce
3
Frozen
Frozen
5
Grocery
Grocery
1
Grocery
Taxable Grocery
10
and Sales
UPC
Department
Category
SubCategory
Sales
1125
2
Fruit
Oranges
20
8256
5
Frozen Treats
Fruit Bars
15
9230
1
Snacks
Chips
28
4018
2
Fruit
Bananas
10
925
2
Vegetables
Onions
9
A relationship is created between the Department and Department Number columns.
I create the following pivot table:
What I want to do is add a measure which shows the total for the Major Department on each row.
I have tried MajorDepartmentSales:=CALCULATE(SUM(Sales[Sales]),ALL(Sales[SubCategory]),ALL(Sales[Category]))
which should remove the filters on category and subcategory. I would expect this to work, however it adds every category under the major department, but with the correct values.
Note, that the value of this measure is correct. It shows the totals under that particular major department. The problem is that it shows every category and subcategory under each major department whether they belong there or not. Why is this?
I have found two ways around this. The first modifies the measure to IF(COUNT(Sales[UPC])>0,CALCULATE(SUM(Sales[Sales]),ALL(Sales[SubCategory]),ALL(Sales[Category])),BLANK()) which checks if there are any items under that particular row, and blanks it out otherwise. The second method is to pull the major department to the Sales table with a calculated column MajorDepartmentOnSales:=Related(Major[Major Department]) and then using this column in the pivot table instead of the major department from the Major table.
Both produce what I want. The IF method seems a bit sketchy to me, however.
Question
My question is then why do I get these extra rows in the original approach?
It seems that DAX is correctly recognizing which major department is in play as it gets the value correct, but it is not recognizing that when it comes to filtering it out of the pivot table. I am really new to DAX, and it seems that I am not understanding something either in how the relationship is propagated down or how power pivot interacts with the pivot table.
How do I solve this? Is there a way to rewrite the measure to not cause these extra rows, or do I have to use one of these alternative methods? Ideally, I don't want to change the model. (The actual data in the real report has more tables and more (slightly different) columns than this example, but the example recreates the essential issue.)
Internal engin produce a cross-join for this combination of column/measure:
SELECT {[Measures].[Sum of Sales],[Measures].[MajorDepartmentSales]}
DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON COLUMNS , NON
EMPTY
Hierarchize(DrilldownMember(DrilldownMember(CrossJoin({[Table1].[Major
Department].[All],[Table1].[Major Department].[Major
Department].AllMembers},
{([Table2].[Category].[All],[Table2].[SubCategory].[All])}),
[Table1].[Major Department].[Major Department].AllMembers,
[Table2].[Category]), [Table2].[Category].[Category].AllMembers,
[Table2].[SubCategory])) DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON ROWS FROM
[Model] CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR,
FORE_COLOR, FONT_FLAGS
If you add a Sales[Department] then you get correct results (what you are expecting), Engine still produce a crossjoin (because of ALL() in measure) but also add a Department (out of crossjoin scoope) and corect relationship are used:
SELECT {[Measures].[Sum of Sales],[Measures].[MajorDepartmentSales]}
DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON COLUMNS , NON
EMPTY
Hierarchize(DrilldownMember(DrilldownMember(DrilldownMember(CrossJoin({[Table1].[Major
Department].[All],[Table1].[Major Department].[Major
Department].AllMembers},
{([Table2].[Department].[All],[Table2].[Category].[All],[Table2].[SubCategory].[All])}),
[Table1].[Major Department].[Major Department].AllMembers,
[Table2].[Department]), [Table2].[Department].[Department].AllMembers,
[Table2].[Category]), [Table2].[Category].[Category].AllMembers,
[Table2].[SubCategory])) DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON ROWS FROM
[Model] CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR,
FORE_COLOR, FONT_FLAGS
You can use such a workaround.
=IF(ISBLANK(SUM(Table2[Sales])); BLANK(); CALCULATE(SUM(Table2[Sales]); ALL(Table2[SubCategory]; Table2[Category])) )

How to count from an looked up range in excel

I have a pivot table that has a row for a set of cities. I want to count the number of cities in a certain state. It's not wanted to include the state in the pivot table (although that would make things simpler).
I tried using the following formula:
={countif(index(city_ref[state], match(G15:G30,city_ref[city_name],0)),"=CA")}
Where:
city_ref is a table that has at least the columns [city_name] and [state]
G15:G30 are the rows of the pivot table
The data looks like this (simplified):
city_ref:
city_name | state
====================
Los Angeles | CA
Seattle | WA
Portland | OR
etc.
G15:G30:
city_name
===========
G15: Los Angeles
G16: Seattle
etc. (but not all of city_ref)
I do realize that you could include the state in a pivot table but a) that's not wanted (even hiding it) and b) there is other, dis-related data that when the state is included the pivot table makes additional rows which messes up the count.
The index match lookup seems to return an array if used on its own; if I use the fill handle it does create an array of cells with the lookup values between G15:G30. However including this in countif does not seem to do anything.
In the help page for countif that I have in excel (unfortunately I cannot find an online reference to link to) it says that range (the first argument) can be an array.
I want to be able to take an arbitrary array of values, look them up in another table and count the ones that match my criteria.
Something like this ?
{=SUM((IFERROR(MATCH($D$2:$D$7,INDEX(IF($B$2:$B$13=$F$2,$A$2:$A$13),),0),0)>0)*1)}
Why don't you just make a copy of the PivotTable, add a Slicer so users can filter it on a particular State, and add the State field to the Values area so that it gets counted? Seems counter productive to point a formula at PivotTable 1 when you could simply have PivotTable2 do the sum by State.

number listing for conditions in sqlite

I have a table in which I am trying to create a column that will increment based on conditions being valid.
In my example, I need to update the department count where the department = Marketing. Anyone have a good way to do this using SQLite?
Current Table
Name Department Department_Count
James Accounting NULL
Jennifer Marketing NULL
Micheal Warehouse NULL
Natalie Marketing NULL
Rebecca Marketing NULL
Update Table
Name Department Department_Count
James Accounting NULL
Jennifer Marketing 1
Micheal Warehouse NULL
Natalie Marketing 2
Rebecca Marketing 3
Edit:
Currently, I insert rows where the department is 'Marketing' into a new table and then I used primary key or rowid to create an auto increment so I can number these types of items.
This requires me to create a new table which is not the best since it takes up so much space and the table is redundant since I have the underlying data in this original table already.
I'm using python to interact with my database, if that helps with solving this problem.
UPDATE
Actually, thinking about it a little further, you may not need a trigger:
INSERT INTO Table (Department, Department_Count)
VALUES (?, (SELECT MAX(IFNULL(Department_Count, 0)+1 FROM Table WHERE Department = ?))
may give you what you want.
Original Answer
You cannot do this declaratively, but you can probably accomplish what you want proceduraly using a trigger.
Two possible strategies:
Use an AFTER INSERT trigger to execute an UPDATE statement against the most recently inserted row (RowID will be available AFTER INSERT) to set the Departement_Count column to a SELECT expression based on the current data in the table.
Use an INSTEAD OF trigger to perform an alternate INSERT combining the values from the NEW cursor with a similar SELECT statement to get the maximum value (plus 1) from the Department_Count column.

New variable in Alteryx without join

So i've created a simple bit of analysis in alteryx, and I'm looking for a bit of advice.
I have a dataset of merchants and shoppers. I pull out the total number of unique shoppers with a summarize node countdistinct(shoppers) - let's say it's 100. Then, I create a table for the number of unique shoppers within each merchant, and that table looks something like this
Merchant | Unique Users
Merchant 1 | 76
Merchant 2 | 19
Merchant 3 | 97
Merchant 4 | 44
Merchant 5 | 55
...
I'd like to create a variable that will be [Number of Distinct Users]/countdistinct(shoppers).
I know that I could just append the value for countdistinct(shoppers) into a new third column in my table, but I'd prefer not to do that unless I have to. Is there a way to save the single number from countdistinct(shoppers) as a value and simply divide directly by that without having to append or join?
There currently is no way to do that in Alteryx. While it feels wrong or wasteful, by including the calculated count distinct value on every record, Alteryx can process each record individually and then pass it down stream.
I have heard talk of creating something like a global variable that could be accessed or updated to handle the situation that you're describing, but the complexity of managing reads and writes from different parts of the workflow as well as any resulting slowdown from thread coordination always sideline the discussion.

Showing unique values in a column along with another value associated with each

I have an Excel table containing transactions of companies. It looks like this:
Customer Phone Item Price
==================================
Company1
Company2
Company1
Company3
Company1
I need to see the unique values of customer column.
The result I want is:
Customer Phone Item Price
==================================
Company1
Company2
Company3
Here is what I tried already:
Remove duplicates: To just get the unique values, I can use the remove duplicates of excel. However, since this is something I will be doing frequently, I would rather not have to make a copy of the table each time in order to delete duplicates.
Pivot table: A pivot table does this job perfectly. My problem here is that I need other column info as well (e.g., the phone number) which I want to appear in the column next to the company. [I haven't yet figured out how to do this in a pivot table (i.e., to show a value as text instead of sum or count etc.)] - This would be the best option for me.
As long as you don't have a super long table where the recalculation will kill you, you could just add a calculated column like this and then filter on value being 1:
=IF(COUNTIF($B$1:B2,"=" & B3) > 0,0,1)
If recalculation is an issue, you can use the same or similar method with VBA except paste static values.
PivotTables are well suited for this kind of stuff, so that'd be my first go-to, but PivotTables are also frustratingly difficult to adapt to uses beyond their envisioned use... so there's a steep decline in their usefulness as you stray from that.
Ultimately, you may want to consider a relational database paradigm, and/or using Access. That's one step up the sophistication ladder towards managing the kind of data you're talking about.

Resources