DAX: Merge two tables selectively (based on a value in a row) - merge

I have two tables:
Table with Division and Entity within divisions
Table with projects identified per entity or for all entities in a division.
**Table 1: Organization**
id Division Entity
- - - - - - - - - - - - -
0 Europe France
1 Europe Germany
2 Europe Italy
3 Europe Spain
4 China North
5 China East
6 China West
7 China South
**Table 2: Project**
id Division Org. Project
- - - - - - - - - - - - - - - - - - - - - - - - - -
1 Europe France Project 1
2 Europe Germany Project 2
3 Europe Germany Project 3
4 Europe All entities Project 4
5 Europe All entities Project 5
6 China East Project 6
7 China All entities Project 7
Some projects are specific to [Org.]'s, while others are relevant for all [Org.]'s in the [Division]. In these cases the value "All entities" is used.
I want to merge both tables based on [Division], but just for Plan[Org.] = "All entities", while the rows with other values (e.g. "France", "Germany") should just be repeated.
The resulting table should look like this:
**Table 3: Merge**
id Division Org. Entity Project
- - - - - - - - - - - - - - - - - - - - - - - - - -
1 Europe France France Project 1
2 Europe Germany Germany Project 2
3 Europe Germany Germany Project 3
4 Europe All entities France Project 4
4 Europe All entities Germany Project 4
4 Europe All entities Spain Project 4
4 Europe All entities Italy Project 4
5 Europe All entities France Project 5
5 Europe All entities Germany Project 5
5 Europe All entities Spain Project 5
5 Europe All entities Italy Project 5
6 China East East Project 6
7 China All entities North Project 7
7 China All entities East Project 7
7 China All entities South Project 7
7 China All entities West Project 7
A physical relationship between the tables doesn't work because of the missing "All entities" value in the Organization table, but it can be added when with an additional [Entity id] column in the Project table, if needed.
I prefer DAX. Solutions in PowerQuery are less preferred as I am not so fluent in M, but still welcome if
available.
Thank you!

Follow these below steps in Power Query-
Step-1: Create a custom column* in table Organization as below-
Here is the output-
Step-2: Create 2 Duplicate table from table Project and name them as - Project_all_emtities and Project_others
Step-3: Go to Advanced Editor for table Project_all_emtities and replace the following code-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXItLcovSAUy3IoS85JBjICi/KzU5BIFQwUFBaVYnWglI2Rl7qlFuYl5lUjqjGDqjPGrM4YqM0FW5piTo5CaV5JZkplajKTWBGamKRGKTaFqzYBizhmZeYkgPYnFJUhKzGDmmSOpwWGcuVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, Division = _t, #"Org." = _t, #"Project " = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"Division", type text}, {"Org.", type text}, {"Project ", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Division", Text.Trim, type text}, {"Org.", Text.Trim, type text}}),
#"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each ([#"Org."] = "All entities")),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Division", "Org."}, Organization, {"Division", "Custom"}, "Organization", JoinKind.LeftOuter),
#"Expanded Organization" = Table.ExpandTableColumn(#"Merged Queries", "Organization", {"Entity "}, {"Organization.Entity "}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Organization",{"id", "Division", "Org.", "Organization.Entity ", "Project "}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Organization.Entity ", "Entity"}})
in
#"Renamed Columns"
Step-4: Go to Advanced Editor for table Project_others and replace the following code-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXItLcovSAUy3IoS85JBjICi/KzU5BIFQwUFBaVYnWglI2Rl7qlFuYl5lUjqjGDqjPGrM4YqM0FW5piTo5CaV5JZkplajKTWBGamKRGKTaFqzYBizhmZeYkgPYnFJUhKzGDmmSOpwWGcuVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, Division = _t, #"Org." = _t, #"Project " = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"Division", type text}, {"Org.", type text}, {"Project ", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Division", Text.Trim, type text}, {"Org.", Text.Trim, type text}}),
#"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each [#"Org."] <> "All entities"),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Division", "Org."}, Organization, {"Division", "Entity "}, "Organization", JoinKind.LeftOuter),
#"Expanded Organization" = Table.ExpandTableColumn(#"Merged Queries", "Organization", {"Entity "}, {"Organization.Entity "}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Organization",{"id", "Division", "Org.", "Organization.Entity ", "Project "}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Organization.Entity ", "Entity"}})
in
#"Renamed Columns"
Step-5: Create a new table By appending Project_all_emtities and Project_others with this below code-
let
Source = Table.Combine({Project_all_emtities, Project_others}),
#"Sorted Rows" = Table.Sort(Source,{{"Division", Order.Descending}, {"Project ", Order.Ascending}})
in
#"Sorted Rows"
Here is the final output-

I have changed the name of the columns, for the table Project. The function CROSSJOIN doesn't like to have duplicate columns names. Organization table is the same.
PROJECT TABLE
+------------+------------------+--------------+-----------+
| id_project | Division_project | Org. | Project |
+------------+------------------+--------------+-----------+
| 1 | Europe | France | Project 1 |
+------------+------------------+--------------+-----------+
| 2 | Europe | Germany | Project 2 |
+------------+------------------+--------------+-----------+
| 3 | Europe | Germany | Project 3 |
+------------+------------------+--------------+-----------+
| 4 | Europe | All entities | Project 4 |
+------------+------------------+--------------+-----------+
| 5 | Europe | All entities | Project 5 |
+------------+------------------+--------------+-----------+
| 6 | China | East | Project 6 |
+------------+------------------+--------------+-----------+
| 7 | China | All entities | Project 7 |
+------------+------------------+--------------+-----------+
The code uses two variables _directrel and _allentity, their outputs correspond to tables. Each one of those uses a CROSSJOIN surrounded by a FILTER statement. The first uses a direct relationship between divisions and entities. The second variable only uses divisions as a link and filtering the Project only for All Entities.
After the RETURN statement, there is a UNION statement, that makes a union between the tables, surrounded by SELECTCOLUMNS to control the order of the columns.
Merge =
VAR _directrel =
GROUPBY (
FILTER (
CROSSJOIN ( Organization, Project ),
[Division] = [Division_project]
&& [Entity] = [Org.]
),
[id_project],
[Division],
[Org.],
[Entity],
[Project]
)
VAR _allentity =
GROUPBY (
FILTER (
CROSSJOIN ( Organization, FILTER ( Project, [Org.] = "All Entities" ) ),
[Division] = [Division_project]
),
[id_project],
[Division],
[Org.],
[Entity],
[Project]
)
RETURN
SELECTCOLUMNS (
UNION ( _allentity, _directrel ),
"id", [id_project],
"Division", [Division],
"Org.", [Org.],
"Entity", [Entity],
"Project", [Project]
)
The output:

Related

Power Query - creating a tracker for multiple studies

New to Power Query and trying to learn how I can use it more to optimize workflows.
Here, I have multiple tables (1 for each study), and am trying to create a tracker that can identify the subject, whether or not they are participating in a specific study, and other data (e.g., study ID). Each table has:
Unique subject identifier (which could be used to link them across studies)
Study-specific identifier
Other misc info (dates of test1, test2, etc)
I am trying to make something that groups the columns of all my tables by the unique subject identifier, and then could have columns for "Study1", "Study 2", "Study 3" with a simple Y/N to show whether or not they are participating in that study. If Y, then also show the study-specific identifier. However, I am stuck.
I've appended all 3 tables into 1 master table - which results in duplicates as a participant can be in multiple studies. When I try the "Group By" function, I group by "Unique subject identifier" with new columns Study1, Study2, Study3, etc and operation "All rows". When I expand these however, it creates multiple duplicate rows, which defeats the purpose of the "Group by" function.
Would appreciate any suggestions you may have.
Example
Each table has some variation of these columns:
Unique participant identifier
Unique Study identifier
Date of Test 1, etc
A
100
01-Apr-2022
B
101
02-Apr-2022
C
102
03-Apr-2022
Let's say Participant A is on study 1 only. Participant B is on Study 2 (study ID 201) and 3 (study ID 301). Participant C is on all 3 (study IDs 102, 202, and 302 respectively).
I am trying to make a table that will show:
Unique participant identifier
Study 1
Study 1 Identifier
Study 2
Study 2 Identifier
Study 3
Study 3 Identifier
A
Y
100
B
Y
101
Y
201
Y
301
C
Y
102
Y
302
Alongside the dates of tests (not shown, but same concept). These tables are updated as we go along, so power query would draw these data from the tables to create a "live" tracker.
The current code I have in the advanced editor is:
let
Source = Table.Combine({STUDY1, STUDY2, STUDY3}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Unique participant identifier"}, {{"STUDY12", each _, type table}, {"STUDY22", each _, type table}, {"STUDY32", each _, type table}}),
#"Expanded STUDY12" = Table.ExpandTableColumn(#"Grouped Rows", "STUDY12", {"STUDY1-Study ID", "STUDY1-ABC Study ID", "STUDY1"}, {"STUDY12.STUDY1-Study ID", "STUDY12.STUDY1-ABC Study ID", "STUDY12.STUDY1"}),
#"Expanded STUDY22" = Table.ExpandTableColumn(#"Expanded STUDY12", "STUDY22", {"STUDY2-Study ID", "STUDY2-OC Study ID", "STUDY2"}, {"STUDY22.STUDY2-Study ID", "STUDY22.STUDY2-ABC Study ID", "STUDY22.STUDY2"}),
#"Expanded STUDY32" = Table.ExpandTableColumn(#"Expanded STUDY22", "STUDY32", {"STUDY3-Study ID", "STUDY3"}, {"STUDY32.STUDY3-Study ID", "STUDY32.STUDY3"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded STUDY32",{" Unique participant identifier ", "STUDY12.STUDY1-Study ID", "STUDY12.STUDY1", "STUDY12.STUDY1-ABC Study ID", "STUDY22.STUDY2", "STUDY22.STUDY2-Study ID", "STUDY22.STUDY2-ABC Study ID", "STUDY32.STUDY3", "STUDY32.STUDY3-Study ID"})
in
#"Reordered Columns"
Done via the "Group by" function. This creates multiple rows for each participant though.
Here's one method. Your sample data is not that great. You also dont mention how to divide the Unique Study identifier into buckets. I assume it is just the integer portion of the number divided 100. This is a bit long to attempt to make the steps obvious
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Unique participant identifier", type text}, {"Unique Study identifier", Int64.Type}, {"Date of Test", type date}}),
// there needs to be some way to identify groups of studies. I am using the hundreds place to find that
#"Added Custom" = Table.AddColumn(#"Changed Type", "StudyNumber", each Number.RoundDown([Unique Study identifier]/100)),
// first table -- studies only
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date of Test"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"StudyNumber", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"StudyNumber", type text}}, "en-US")[StudyNumber]), "StudyNumber", "Unique Study identifier", List.Sum),
// second tabe -- study dates only
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom",{"Unique Study identifier"}),
#"Pivoted Column1" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns2", {{"StudyNumber", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns2", {{"StudyNumber", type text}}, "en-US")[StudyNumber]), "StudyNumber", "Date of Test"),
RenamedColumns = Table.TransformColumnNames(#"Pivoted Column1", each if _="Unique participant identifier" then _ else _&" Date"),
#"Removed Columns1" = Table.RemoveColumns(RenamedColumns,{"Unique participant identifier"}),
// combine the tables
combined = Table.FromColumns(Table.ToColumns(#"Pivoted Column")&Table.ToColumns( #"Removed Columns1"),Table.ColumnNames(#"Pivoted Column") &Table.ColumnNames(#"Removed Columns1"))
in combined
code version for yes/no
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Unique participant identifier", type text}, {"Unique Study identifier", Int64.Type}, {"Date of Test", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "StudyNumber", each Number.RoundDown([Unique Study identifier]/100)),
// first table -- studies only
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date of Test"}),
#"Rounded Off" = Table.TransformColumns(#"Removed Columns",{{"StudyNumber", each Text.From(_) &" Identifier"}}),
Part1 = Table.Pivot(Table.TransformColumnTypes(#"Rounded Off", {{"StudyNumber", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Rounded Off", {{"StudyNumber", type text}}, "en-US")[StudyNumber]), "StudyNumber", "Unique Study identifier", List.Sum),
// 2nd table -- YN only
#"Change to Y" = Table.TransformColumns(#"Removed Columns",{{"Unique Study identifier",each "Y" }}),
#"Rounded Off1" = Table.TransformColumns(#"Change to Y",{{"StudyNumber", each "Study " &Text.From(_)}}),
Pivot = Table.Pivot(Table.TransformColumnTypes(#"Rounded Off1", {{"StudyNumber", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Rounded Off1", {{"StudyNumber", type text}}, "en-US")[StudyNumber]), "StudyNumber", "Unique Study identifier"),
Part2 = Table.RemoveColumns(Pivot,{"Unique participant identifier"}),
// 3rd table -- study dates only
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom",{"Unique Study identifier"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns2", {{"StudyNumber", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns2", {{"StudyNumber", type text}}, "en-US")[StudyNumber]), "StudyNumber", "Date of Test"),
RenamedColumns3 = Table.TransformColumnNames(#"Pivoted Column", each if _="Unique participant identifier" then _ else _&" Date"),
Part3 = Table.RemoveColumns(RenamedColumns3,{"Unique participant identifier"}),
// combine the tables
combined = Table.FromColumns(Table.ToColumns(Part1)&Table.ToColumns( Part2)&Table.ToColumns( Part3),Table.ColumnNames(Part1) &Table.ColumnNames(Part2)&Table.ColumnNames(Part3))
in combined

Merge queries matching on dates and leave null if no match found

I have got two tables below (examples):
Table SFID
Sales Force ID
Type
Name
Assistant
From
To
123
ABC
Store A
Ben
01/04/2020
30/04/2020
123
ABC
Store A
Jen
01/05/2020
31/05/2020
123
ABC
Store A
Ben
01/06/2020
21/06/2020
126
DEF
Store B
Tim
01/04/2020
30/04/2020
126
DEF
Store B
Tim
01/04/2020
null
and
Table Activity
Transaction ID
Date
Sales Force ID
1
03/05/2020
123
2
03/06/2020
200
3
01/01/2021
123
4
02/01/2021
126
I want my end result to be
Transaction ID
Date
Sales Force ID
Type
Name
Assistant
1
03/05/2020
123
ABC
Store A
Jen
2
03/06/2020
200
null
null
null
3
01/01/2021
123
null
null
null
4
02/01/2021
126
DEF
Store B
Tim
To do this, the best solution was the one posted in here with some modifications (allow both To and From to be null). However, only the row on transaction ID 2 disappears because that Sales Force had already had assistant entries (they get wiped out on the filtered row step). I also tried the solution presented in here but it takes ages to load.
I would like to know if there was a way to guarantee all transactions appear without having to introduce lines to table SFID for periods they don't have assistants and without making the query a really slow one.
This is my code:
let
Source = Source,
#"Merged Queries" = Table.NestedJoin(Source,{"Sales Force ID"},SFID,{"SFID"},"SFID",JoinKind.LeftOuter),
#"Expanded SFID" = Table.ExpandTableColumn(#"Merged Queries", "SFID", {"Type", "Name", "Assistant", "From", "To"}, {"Type", "Name", "Assistant", "From", "To"}),
#"Changed Type" = Table.TransformColumnTypes(#"SFID",{{"Date", type date}, {"From", type date}, {"To", type date}}),
FilteredRows = Table.SelectRows(#"Changed Type", each ([Date] >= [From] and [Date] <= [To]) or ([Date] >= [From] and [To] = null)or ([From] = null and [To] = null)),
#"Removed Columns" = Table.RemoveColumns(FilteredRows,{"From", "To"})
in
#"Removed Columns"
So after almost 2 weeks with no response and an unjustified downgrade, I found a solution!
I created a query that would basically produce this table with the above mentioned code.
Transaction ID
Date
Assistant
1
01/01/2021
Jen
4
02/01/2021
Tim
let
Source = Source,
#"Merged Queries" = Table.NestedJoin(Source,{"Sales Force ID"},SFID,{"SFID"},"SFID",JoinKind.LeftOuter),
#"Expanded SFID" = Table.ExpandTableColumn(#"Merged Queries", "SFID", {"Type", "Name", "Assistant", "From", "To"}, {"Type", "Name", "Assistant", "From", "To"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded SFID",{{"Date", type date}, {"From", type date}, {"To", type date}}),
FilteredRows = Table.SelectRows(#"Changed Type", each ([Date] >= [From] and [Date] <= [To]) or ([Date] >= [From] and [To] = null)),
#"Removed Columns" = Table.RemoveColumns(FilteredRows,{"From", "To"})
in
#"Removed Columns"
And left-joined it on my initial version of Table Activity
I kept the information of type Type and Name in a separate Query (as they don't change) and then again left-joined it on the Table Activity.
Final query looks like this:
let
Source = Source,
#"Merged Queries1" = Table.NestedJoin(Source,{"Sales Force Code"},Info,{"SFID"},"Info",JoinKind.LeftOuter),
#"Expanded Info" = Table.ExpandTableColumn(#"Merged Queries1", "Info", {"Type", "Name"}, {"Type", "Name"}),
#"Merged Queries" = Table.NestedJoin(#"Expanded Info",{"ID"},IDvsAssistant,{"ID"},"IDvsAssistant",JoinKind.LeftOuter),
#"Expanded IDvsAssistant" = Table.ExpandTableColumn(#"Merged Queries", "IDvsAssistant", {"Assistant"}, {"Assistant"})
in
#"Expanded IDvsAssistant"

Difference of averages of columns in 2 tables in Postgres database

I have split a table into 2 based on the average value of a column and now I want to calculate the difference in the average of the other columns in the new tables.
e.g. (simplified example, my data has 5 columns)
t1 = 'id', 'price', 'size'
1 , 10.0, 3
..., ..., ...
10, 6.50, 8
t2 = 'id', 'price', 'size'
11 , 12.12, 3
..., ..., ...
20, 3.50, 5 '
diff_table = 'diff_price' ,'diff_size'
t1.avg_price - t2.avg_price, t1.avg_size - t2.avg_size
Hm, pretty simple as:
SELECT
avg(t1.price) - avg(t2.price) AS diff_price,
avg(t1.size) - avg(t2.size) AS diff_size
FROM t1, t2;
Example: https://www.db-fiddle.com/f/eXuc1bUyxAHhWo2Yu91GMt/0
If you need that data to be put into a new table, just do the following )with explicit column typescale):
CREATE TABLE diff_table AS
SELECT
(avg(t1.price) - avg(t2.price))::decimal(10,2) AS diff_price,
(avg(t1.size) - avg(t2.size))::decimal(10,2) AS diff_size
FROM t1, t2;
...result:
Table "public.diff_table"
Column | Type | Collation | Nullable | Default
------------+---------------+-----------+----------+---------
diff_price | numeric(10,2) | | |
diff_size | numeric(10,2) | | |
Okay, I resolved my own problem.
CREATE TABLE diff AS
SELECT AVG(bathroomcnt) AS avg_bathroomcnt,
AVG(bedroomcnt) AS avg_bedroomcnt,
AVG(calculatedfinishedsquarefeet) AS avg_calculatedfinishedsqrft,
AVG(garagecarcnt) AS avg_garagecarcnt,
AVG(lotsizesquarefeet) AS avg_lotsizesqft,
AVG(roomcnt) AS avg_roomcnt
FROM higher_values;
CREATE TABLE diff2 AS
SELECT AVG(bathroomcnt) AS avg_bathroomcnt,
AVG(bedroomcnt) AS avg_bedroomcnt,
AVG(calculatedfinishedsquarefeet) AS avg_calculatedfinishedsqrft,
AVG(garagecarcnt) AS avg_garagecarcnt,
AVG(lotsizesquarefeet) AS avg_lotsizesqft,
AVG(roomcnt) AS avg_roomcnt
FROM lower_values;
SELECT h.avg_bathroomcnt - l.avg_bathroomcnt AS bathroomcnt_diff,
h.avg_bedroomcnt - l.avg_bedroomcnt AS bedroomcnt_diff,
h.avg_calculatedfinishedsqrft - l.avg_calculatedfinishedsqrft AS calculatedfinished_diff,
h.avg_garagecarcnt - l.avg_garagecarcnt AS garagecarcnt_diff,
h.avg_lotsizesqft - l.avg_lotsizesqft AS lotsizesqft_diff,
h.avg_roomcnt - l.avg_roomcnt AS roomcnt_diff
from diff as h, diff2 as l;

find the next record that contains [some stuff]

I'm working on a report that contains inpatient ("IP") surgical visits and the service dates of the follow-up x-ray visits, which is based on patient type and revenue code:
MRN AdmitDate Pattype RevCode ServiceDate
123 1/1/2015 IP 100 *null*
123 *null* PT 200 2/1/2015
123 *null* SVO 320 2/10/2015
123 *null* PT 200 2/15/2015
I'm trying to roll up rows 1 and 3 on a single line to appear as follows:
MRN AdmitDate Pattype FollowUp
123 1/1/2015 IP 2/10/2015
but am getting either an empty return or just the next record in the dataset using #followup =
If {encounter.pattype} = "IP" then
if next ({encounter.pattype}) in [several different patient types]
if {charge_detail.revenuecode} in ["0320" to "0324"] then
{charge_detail.servicedate}

Selection formula excluding rows with columns having null values

I have a strange issue. I have a report CR. In the Selection Formula I do a test on two fields. The test is simple like that : {field_City} = 'Paris' OR {field_Country} = 'France'.
This is a sample of the data in my table:
|---------------|---------------|---------------|
| ID_Record | Country | City |
|---------------|---------------|---------------|
| 1 | null | Paris |
|---------------|---------------|---------------|
| 2 | France | null |
|---------------|---------------|---------------|
| 3 | France | Paris |
|---------------|---------------|---------------|
The result of the Selection should be the 3 records, however it's excluding the 2 first rows where there is a null value in one of the columns. Then I changed the Selection Formula like this to consider null values too : ({field_City} = 'Paris' AND (isnull({field_Country}) OR not(isnull({field_Country})))) OR ({field_Country} = 'France' AND (isnull({field_City}) OR not(isnull({field_City})))) but I am still getting only the last record ! To ensure myself that my code is correct, I generated the sql query via the option in CR 'Show sql query', then i've added a WHERE clause in which I wrote the same condition that i've put in the Selection Formula, and...... it gave me the 3 records ! Unfortunately I can't work with the sql query, I have to find out why the formula is excluding the records that have a null value in one of the columns :( I hope that you can help me. Thanks a lot !
This is the solution: ((isnull({field_Country}) AND {field_City} = 'Paris') OR (isnull({field_City}) AND {field_Country} = 'France') OR (not(isnull({field_Country})) AND {field_City} = 'Paris') OR (not(isnull({field_City})) AND {field_Country} = 'France')) , Thank you so much Craig!
You need to test for null values first:
( Not(Isnull({field_Country})) AND {field_Country}='France' )
OR
( Isnull({field_Country}) AND {field_City}='Paris' )