Forum Discussion
IF with multiple statements in M language
Which other details do you need?
I asked if it is possible to write in M language a such IF with a statement block:
IF <condition>
BEGIN
<statement 1>
<statement 2>
<statement 3>
...
<statement n>
END
In other programming language this is possible (e.g. C, C++, T-SQL, etc.).
Thanks
Hi Anonymous ,
The description in your post is too general,since we have shown you plenty of references which should be helpful,if they dont help,show us the details for the [statement],you could paste some sample data with expected output to let us know your requirement more clear.
Or you could paste a sql query such as below:
DECLARE @sales INT;
SELECT @sales = SUM(OrderQty * UnitPrice)
FROM [AdventureWorks2017].[Sales].[SalesOrderDetail];
IF @sales > 100000000
SELECT *
FROM [AdventureWorks2017].[Sales].[SalesOrderDetail];
ELSE
SELECT *
FROM [AdventureWorks2017].[Sales].[SalesOrderHeader];
Best Regards,
Kelly
Did I answer your question? Mark my reply as a solution!
- Anonymous5 years agoNot applicable
Hi,
my question is simply: "is it possible to write an IF with a statement block?". I'd like to know the capabilities of the IF construct, also without indicating a specific case.
As I said, T-SQL allows to use the IF construct with a statement block:
USE AdventureWorks2012; GO DECLARE @AvgWeight DECIMAL(8,2), @BikeCount INT IF (SELECT COUNT(*) FROM Production.Product WHERE Name LIKE 'Touring-3000%' ) > 5 BEGIN SET @BikeCount = (SELECT COUNT(*) FROM Production.Product WHERE Name LIKE 'Touring-3000%'); SET @AvgWeight = (SELECT AVG(Weight) FROM Production.Product WHERE Name LIKE 'Touring-3000%'); PRINT 'There are ' + CAST(@BikeCount AS VARCHAR(3)) + ' Touring-3000 bikes.' PRINT 'The average weight of the top 5 Touring-3000 bikes is ' + CAST(@AvgWeight AS VARCHAR(8)) + '.'; END ELSE BEGIN SET @AvgWeight = (SELECT AVG(Weight) FROM Production.Product WHERE Name LIKE 'Touring-3000%' ); PRINT 'Average weight of the Touring-3000 bikes is ' + CAST(@AvgWeight AS VARCHAR(8)) + '.' ; END ; GO- v-kelly-msft5 years agoCommunity Support
Hi Anonymous ,
I see.
You could use below structure do to the statement block:
If (condition A),
then let {statement}
in (output)
else let {statement}
in (output)
Below is the reference for let formular:
https://docs.microsoft.com/en-us/powerquery-m/m-spec-let
Best Regards,
KellyDid I answer your question? Mark my reply as a solution!