I have a field which is named: grupo it contains this values: Repairs & Maintenance . . . a lot of values I want to choose just the important thing and group all the other in autres: Other and calculate the total.
How can I do that in qlikview
I can advise you to make this in the script - create new field in the same table where grupo field is. The new field should be based on mapping table or something like this: if( match( grupo, 'ICMS','PENALTY' ) > 0, grupo, 'Other') as grupo1
Related
I'm somewhat new to PowerApps and I'm stuck at trying to reproduce a JOIN using the AddColumns function and a LookUp function as I saw on this link: Syntax for joining tables. This JOIN will go inside of a Data Table.
I have two SQL tables I'm trying to JOIN using that method
mT_SalesAttributeDB_FamilyMaterialGroupMaterials: ([ID], [MaterialGroupID], [MaterialID], [Comment], [Created], [Updated])
mT_SalesAttributeDB_Materials: ([ID], [MaterialName], [Description], [ShortDesc], [Abbr], [Superseded], [HasNoDrawing], [Created], [Updated])
What I'm wanting to accomplish is this:
SELECT
*
FROM
mT_SalesAttributeDB_FamilyMaterialGroupMaterials AS Grps
INNER JOIN
mT_SalesAttributeDB_Materials AS Mats ON Grps.MaterialID = Mats.ID
WHERE
Grps.MaterialGroupID = tblMaterialGroups_Families.Selected.ID
I think this is what that should look like:
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
Text(tblMaterialGroups_Families.Selected.ID) = MaterialGroupID
),
"MaterialName",
LookUp(
mT_SalesAttributeDB_Materials,
ID = mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID],
MaterialName
)
)
However, the equality inside of the LookUp, ID = mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID] gives an error on the equal sign saying I can't compare a number to a table
As a reference, I've tried this and it works fine
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
Text(tblMaterialGroups_Families.Selected.ID) = MaterialGroupID
)
Is the problem that I'm trying to use a preview object that isn't fully released? Or am I joining them incorrectly?
EDIT
I've also tried this and still get the same error about not being able to compare a number to a Table
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialGroupID] = Text(tblMaterialGroups_Families.Selected.ID)
),
"SpecName",
LookUp(
mT_SalesAttributeDB_Materials,
mT_SalesAttributeDB_Materials[#ID] = mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID],
mT_SalesAttributeDB_Materials[#MaterialName]
)
)
NOTE: I also wanted to mention that I attempted the exact same AddColumns with a List Box to make sure it wasn't something to do with Data Tables being in preview, and I still get the error that I can't compare a Number to a Table
EDIT 2
I seem to be getting close. I can get the error to stop and it to populate "SpecName", however, it just grabs the first MaterialName from mT_SalesAttributeDB_Materials instead of doing a proper LookUp
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
MaterialGroupID = Text(tblMaterialGroups_Families.Selected.ID)
),
"MaterialName",
LookUp(
mT_SalesAttributeDB_Materials,
ID in mT_SalesAttributeDB_FamilyMaterialGroupMaterials.MaterialID,
MaterialName
)
)
I changed the "=" to an "in" and it seems that mT_SalesAttributeDB_FamilyMaterialGroupMaterials.MaterialID is the same as mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID]
Okay, so I finally got it with some help from Google. I had a few things that were likely causing me problems with my tables.
_Materials.MaterialID was changed to .MaterialName so I wouldn't have two fields with the same name but different meanings. I think that helped when PowerApps was using context.
_Materials.ID is stored as an INT however, for some reason I decided to store _FamilyMaterialGroupMaterials as a VARCHAR. So I changed that to an INT so MaterialID and ID would be exactly the same thing in each table. (I assume this helps with the JOIN)
Once I had that updated, this is the correct syntax to JOIN two tables. You can see I even added in a second Column once I got it working
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
MaterialGroupID = tblMaterialGroups_Families.Selected.ID
),
"MaterialName",
LookUp(
mT_SalesAttributeDB_Materials,
ID = MaterialID
).MaterialName,
"ShortDesc",
LookUp(
mT_SalesAttributeDB_Materials,
ID = MaterialID
).ShortDesc
)
Hopefully my full day wasted trying to figure that out will help someone else.
say, I have a result of rows of a member from a simple query.
select distinct mbr_id from mbr_base where location = '17957' ;
result would look like this.
mbr_id
location
000000011441894
17957
000000011437056
17957
000000011437981
17957
000000011441312
17957
000000011440730
17957
000000011482555
17957
000000011498476
17957
this is one of the result where location condition is filtered.
Yet, I have another 49 locations to iterate as these are my distinct locations to be examined.
Finally, I would combine all this as a one table as a result table to be ready for analytics.
For example, my Psuedo-code for Python would like
df = pd.DataFrame()
for i in unique(mbr_base['location']):
rst = '''select * from where location = 'i'; '''
rst_df = pd.to_dataframe(rst)
pd.concat([df,rst_df],axis=0)
display(df)
Can you help me to write a procedure for doing this in sql(pgsql preferrably)
Many thanks;
If you want the individual locations, then use in:
select distinct mbr_id, location
from mbr_base
where location in ( '17957', . . . )
Your sample results have the location. If you just want the mbr_id, then use:
select distinct mbr_id
from mbr_base
where location in ( '17957', . . . )
Now, presumably mbr_id is unique in mbr_base. If so, remove the distinct. In addition, location looks like a number. If it really is a number, then drop the single quotes. So, what you might want is:
select mbr_id
from mbr_base
where location in ( 17957, . . . )
Can you please help me to get the item with the highest count using DAX?
Measure = FIRSTNONBLANK('Table1'[ItemName],CALCULATE(COUNT('Table2'[Instance])))
This shows the First ItemName in the table but doesnt get the ItemName of the Highest Value.
Thanks
Well, it's more complicated than I would have wanted, but here's what I came up with.
There things that you are hoping to do that are not so straightforward in DAX. First, you want an aggregated aggregation ;) -- in this case, the Max of a Count. The second thing is that you want to use a value from one column that you identify by what's in another column. That's row-based thinking and DAX prefers column-based thinking.
So, to do the aggregate of aggregates, we just have to slog through it. SUMMARIZE gives us counts of items. Max and Rank functions could help us find the biggest count, but wouldn't be so useful for getting Item Name. TOP N gives us the whole row where our count is the biggest.
But now we need to get our ItemName out of the row, so SELECTCOLUMNS lets us pick the field to work with. Finally, we really want a value not a 1-column, 1-row table. So FirstNonBlank finishes the job.
Hope it helps.
Here's my DAX
MostFrequentItem =
VAR SummaryTable = SUMMARIZE ( 'Table', 'Table'[ItemName], "CountsByItem", COUNT ( 'Table'[ItemName] ) )
VAR TopSummaryItemRow = TOPN(1, SummaryTable, [CountsByItem], DESC)
VAR TopItem = SELECTCOLUMNS (TopSummaryItemRow, "TopItemName", [ItemName])
RETURN FIRSTNONBLANK (TopItem, [TopItemName])
Here's the DAX without using variables (not tested, sorry. Should be close):
MostFrequentItem_2 =
FIRSTNONBLANK (
SELECTCOLUMNS (
TOPN (
1,
SUMMARIZE ( 'Table', 'Table'[ItemName], "Count", COUNT ( 'Table'[ItemName] ) ),
[Count], DESC
),
"ItemName", [ItemName]
),
[ItemName]
)
Here's the mock data:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcipNSspJTS/NVYrVIZ/nnFmUnJOKznRJzSlJxMlyzi9PSs3JAbODElMyizNQmLEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Stuff = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Stuff", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Stuff", "ItemName"}})
in
#"Renamed Columns"
I am using Visual Studio 2017 and I installed the latest crystal reportviewer (22)
What I want is to click a button and create a report from the customer that is selected in the datagridview and the addresses that are shown in the second datagridview.
I managed to do all that but the problem is that a few fields contain numbers which need to be converted to text. An SQL query I would use to do this would be like:
SELECT c.customer_nr, c.status, s.rename FROM CUSTOMERS c INNER JOIN SETUP s on s.id = c.status WHERE s.afk = 'STA'
In my SETUP database I have the columns ID,AFK and RENAME so if the status would be 1 it would convert to text: "ACTIVE", if status = 2 it would convert to "INACTIVE" for example.
I could do something with a formula field like this:
IF ({c.status} = 1) THEN "ACTIVE" ELSE
IF ({c.status}) = 2 THEN "INACTIVE"
but that is not good because i could add another status or change the name in the database etc.
So then I tried with an SQL expression field and I put something like this:
(
SELECT "SETUP"."RENAME" FROM SETUP
WHERE "SETUP"."AFK" = 'STA' AND "SETUP"."ID" = "CUSTOMERS"."STATUS"
)
There must be something wrong because I get the correct conversion but there is only one address in the database but I get 7 pages all with the same address. There should only be one address like I get when I remove the SQL expression field. Where does it go wrong?
* EDIT *
I found the problem. When I create a new database that contains only unique id's then it works. In my original database I have multiple times the id's 1,2,3,4,5 but with different abbreviations in column AFK. Somehow the query looks for the id value and every time it finds this id no matter the AFK value it generates an entry for the address value.
Maybe in the future I will find out how this exactly works for now I have a workaround.
Create a new table for example CrRepSta and add the following entries:
ID,AFK,RENAME
1,STA,Active
2,STA,Inactive
etc
The new query:
(
SELECT "CrRepSta"."RENAME" FROM CrRepSta
WHERE "CrRepSta"."AFK" = 'STA' AND "CrRepSta"."ID" = "CUSTOMERS"."STATUS"
)
And by the way the statement "CrRepSta"."AFK" = 'STA' is not really needed.
I'm looking to add a note to each of my accounts whenever I make contact via a mass email. I'm looking at something like this so far:
INSERT INTO Customer_notes (Note_text)
VALUES ('emailed June 1st')
WHERE Customer_Level = 'Alpha'
This obviously doesn't work, and UPDATE/SET will replace all of my previous notes.
Well, you could do something like this:
INSERT INTO Customer_notes (Note_text)
Note_Text = COALESCE(Note_text + '
', '') + 'emailed June 1st')
WHERE Customer_Level = 'Alpha';
However, it seems like you need more than one note per customer. So perhaps:
INSERT INTO Customer_notes (Customer_Level, Note_text)
VALUES ('Alpha', Note_Text);
This is assuming that Customer_Level is some sort of customer id.
If you mean add that text to whatever currently exists in Note_text for existing rows, then try this..
UPDATE Customer_notes
SET Note_text = ISNULL(Note_text, '') + ' emailed June 1st'
WHERE Customer_Level = 'Alpha'
{edit based on the fifth comment in this answer}
Ok, then instead of VALUES we are SELECTing off of the same table. Try this (Holy Gawd make a backup first) if the idea is to create another row with EVERY row WHERE Customer_Level = 'Alpha' in this table.
INSERT INTO Customer_Notes (CustomerID, note_text)
SELECT CustomerID, 'emailed June 1st'
FROM CustomerNotes
WHERE Customer_Level = 'Alpha'
This does beg the question if Customer_Level is a subset of all customers, and if the idea here is to only insert one row for EACH of these customers, as opposed to ONE row for each customber/existing rows. You'll have to describe this further for us if this is the case.