Validation Rule for Opportunity - validationrules

I’m trying to create a validation rule that would prevent any non-Integration users from making changes to the Amount or Date fields when an Opportunity is marked as Closed Won OR Lost and IF Custom Field 1 OR 2 is not Null. Also, this only applies to our Special record type. It isn’t firing though, any ideas or help would be appreciated. Thank you! #Validation Rule #Validation Rules
AND(
OR(
TEXT( StageName ) = "Closed Lost",
TEXT( StageName ) = "Closed Won",
ISCHANGED( Amount ),
ISCHANGED( Date ),
NOT( ISBLANK ( Custom Field 1 )),
NOT( ISBLANK ( Custom Field 2 )),
RecordType.Name = “Special”,
( !$User.Is_Integration_User ))
)

Related

AddColumns says I'm comparing a number to a table

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.

Get the item with the highest count

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"

Check if column is null or blank in DAX

EVALUATE
FILTER
(
SUMMARIZE (
NATURALLEFTOUTERJOIN (
'Target_Category',
'Target_Form'
),
'Target'[Area],
'Target'[id],
'Target'[Target date],
'Target'[Target Time Range],
'Target_Category'[Origin],
'Target_Category'[Sectotion],
'Target'[Location],
'Target_Category'[Ethencity],
'Target_FormResponse'[Area Used],
'Target'[Description]
),
'Target'[id] = Value("111373268")
)
ORDEr BY 'Target'[Target Key]
I have the sample DAX query above. Is there away i can manipulate 'Target_FormResponse'[Area Used] such that if it is blank or empty, i return "No" otherwise if its not blank or empty i return "Yes".
In SSRS, i can do something like =IIF(Len(Fields!Form_Response.Value) > 0,"Yes","No") but i want to achieve this at the DAX query level.
If you are satisfied with adding an extra column that contains the "Yes" or "No" values, simple wrap the entire expression in a call to ADDCOLUMNS:
EVALUATE
ADDCOLUMNS (
FILTER (
SUMMARIZE (
NATURALLEFTOUTERJOIN ( 'Target_Category', 'Target_Form' ),
'Target'[Area],
'Target'[id],
'Target'[Target date],
'Target'[Target Time Range],
'Target_Category'[Origin],
'Target_Category'[Sectotion],
'Target'[Location],
'Target_Category'[Ethencity],
'Target_FormResponse'[Area Used],
'Target'[Description]
),
'Target'[id] = VALUE ( "111373268" )
),
"Area Used Yes/No", IF ( 'Target_FormResponse'[Area Used] > 0, "Yes", "No" )
)
ORDER BY 'Target'[Target Key]
If you want to get rid of the original column in the output, you'd have to use SELECTCOLUMNS instead, but unfortunately, you'd then have to specify the names of each of the columns you want to keep, so the code ends up a lot longer.

Where Clause using another Dimension Value

I'm trying to create a Calculated Member in my cube with where clause but couldn't figure how to achieve the proper result.
I created a calculated member "Outlook" using the below code to display only Forecast values.
CREATE MEMBER CURRENTCUBE.[Measures].[Outlook]
AS SUM(([Source Profile].[Source Profile Hierarchy].CurrentMember,
[Source Profile].[Profile Level01].&[Outlook]),
[Measures].[USD Amount]),
FORMAT_STRING = "#,##0;(#,##0)",
VISIBLE = 1 , DISPLAY_FOLDER = 'USD' , ASSOCIATED_MEASURE_GROUP = 'CARS';
Cube Result
Now I would like to filter the results dynamically based on another hidden dimension "Current_Month". This dimension always has current financial period value and it's corresponding outlook profile
Year_Month Outlook_Profile
2015010 10 + 2
Expected result should be "Outlook" measure showing value based on Current_Month dimension, which is '10 + 2' and rest of them should be 0
Expected result
Just to explain the requirement in SQL terms, I would like to achieve the below in MDX
Where Fact.Source_Profile=Dimension.Source_Profile
instead of
Where Fact.Source_Profile='10 + 2'
I'm not sure how to achieve this in Where Clause or by another means. I could see examples of hard coding values, like year.&[2015] but haven't seen one using dynamic values.
I found a solution myself and thought of sharing the same. Used StrToMember function to pass hidden dimension's member as a variable here.
CREATE MEMBER CURRENTCUBE.[Measures].[Outlook]
AS (
Sum
(
(
[Source Profile].[Source Profile Hierarchy].CurrentMember,
strtomember("[Source Profile].[Source Name].&[" + [Outlook Profile].[Outlook Profile].&[1].Member_Caption + "]")
)
,[Measures].[USD Amount]
)
),
FORMAT_STRING = "#,##0;(#,##0)",
VISIBLE = 1 , DISPLAY_FOLDER = 'USD' , ASSOCIATED_MEASURE_GROUP = 'CARS' ;

Case, Select conditional requirments

Operating in an Excel query, I need a conditional statement that will read one field, and based on that value, set another field to a minus (or not).
My SQl code is as follows:
SELECT "_bvSTTransactionsFull".txdate,
SUM("_bvSTTransactionsFull".debit) AS 'TOTALDebit',
SUM("_bvSTTransactionsFull".credit) AS 'TOTALCredit',
SUM("_bvSTTransactionsFull".tax_amount) AS 'TOTALTax_Amount',
SUM("_bvSTTransactionsFull".VALUE) AS 'TOTALValue',
SUM("_bvSTTransactionsFull".actualvalue) AS 'TOTALActualValue',
SUM("_bvSTTransactionsFull".actualsalesvalue) AS 'TOTALActualSalesValue',
SUM("_bvSTTransactionsFull".profit) AS 'TOTALProfit'
FROM sqlschema.dbo."_bvSTTransactionsFull" "_bvSTTransactionsFull"
WHERE ( "_bvSTTransactionsFull".txdate >=?
AND "_bvSTTransactionsFull".txdate <=? )
GROUP BY "_bvSTTransactionsFull".txdate,
"_bvSTTransactionsFull".description
HAVING ( "_bvSTTransactionsFull".description LIKE 'POS Sale' )
OR ( "_bvSTTransactionsFull".description LIKE 'POS Return' )
ORDER BY "_bvSTTransactionsFull".txdate
I need the select query to look at a field named "ActualQuantity" (in the table "_bvSTTransactionsFull") and if this field is <0 , then Tax_Amount = -(Tax_Amount), or if ActualQuantity >=0, then Tax_Amount = Tax_Amount.
Please note the query is "summed", so I assume this conditional aspect needs to be handled before summation takes place. The query summates approximately 100 000 records into daily totals.
SELECT "_bvSTTransactionsFull".txdate,
SUM("_bvSTTransactionsFull".debit) AS 'TOTALDebit',
SUM("_bvSTTransactionsFull".credit) AS 'TOTALCredit',
SUM(case when "_bvSTTransactionsFull".ActualQuantity >= 0
then "_bvSTTransactionsFull".tax_amount
else - "_bvSTTransactionsFull".tax_amount
end) AS 'TOTALTax_Amount',
SUM("_bvSTTransactionsFull".VALUE) AS 'TOTALValue',
SUM("_bvSTTransactionsFull".actualvalue) AS 'TOTALActualValue',
SUM("_bvSTTransactionsFull".actualsalesvalue) AS 'TOTALActualSalesValue',
SUM("_bvSTTransactionsFull".profit) AS 'TOTALProfit'
FROM sqlschema.dbo."_bvSTTransactionsFull" "_bvSTTransactionsFull"
WHERE ( "_bvSTTransactionsFull".txdate >=?
AND "_bvSTTransactionsFull".txdate <=? )
GROUP BY "_bvSTTransactionsFull".txdate,
"_bvSTTransactionsFull".description
HAVING ( "_bvSTTransactionsFull".description LIKE 'POS Sale' )
OR ( "_bvSTTransactionsFull".description LIKE 'POS Return' )
ORDER BY "_bvSTTransactionsFull".txdate
If ActualQuantity might be zero but taxAmount in the same row is zero too you can use sign function to change sign of tax_amount:
sign(ActualQuantity) * tax_amount
UPDATE:
So I gather that MS Query has problems using parameters in a query that cannot be displayed graphically. Workaround is to replace parameters with some constants, save workbook as XML and change constants to "?". There is a link showing VBA code which does replacement on-site, but you will have to figure out how it works as I don't know VBA that much.
UPDATE 2:
On the other hand the easiest way out is to create view in your database.