Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Help with common fields and synthetic keys

Hello everyone,

I'm having trouble figuring out how to link my tables together so that I can use one filter to select on all data. I have two common fields, School and Gender in both of my main tables and want to be able to make selections / comparisons across all data.

My current data model looks like this:

TableStructure.JPG

I have some bookings data that looks like this:

BookingsKeyEnrol IDCourse CodeStudent IDGenderDate FromDate ToFlagGLSchool
66146-YLE166146YLE152003M06/07/1717/07/170L
67541-YBE1J67541YBE1J53282F03/07/1714/07/170B
67561-YBE167561YBE153296M03/07/1714/07/170B
69338-YLE169338YLE154953M06/07/1717/07/170L
69338-YLE269338YLE254953M20/07/1731/07/170L
75823-YBIAE275823YBIAE260839F03/07/1728/07/170B
74851-YLE174851YLE160090F06/07/1717/07/171L
74306-YBE174306YBE159594M03/07/1714/07/171B

Each School has a fixed capacity per gender which is held in a separate table called YL:

YL.DateWeekYearYL.SchoolYL.CategoryYL.GenderYL.Capacity
02/07/201727-2017BGL BedsM2
02/07/201727-2017BGL BedsF2
02/07/201727-2017BTotal BedsM150
02/07/201727-2017BTotal BedsF159
05/07/201727-2017LGL BedsM1
05/07/201727-2017LGL BedsF4
05/07/201727-2017LTotal BedsM99
05/07/201727-2017LTotal BedsF154

(The bookings_x_dates table is created by a while loop that generates all the possible dates for each booking using the DateFrom and DateTo fields - )

Ultimately I want to be able to filter on School and Gender to see how many students/GLs we have on any given date/week compared to how many beds are remaining in the school in a table similar to the below:

CapacitiesTable.JPG

Obviously one of the problems is that the two bottom rows, no. of GLs and no. of students are not being split by gender because the two tables are not linked properly. If I unqualify the gender and school fields in the YL table though this causes chaos with links and synthetic keys (see below).... How can I get around this problem? Would a link table be the way forward and if so, how would I go about that?

Synthetic Key and Loops.JPG

23 Replies
CarlosAMonroy
Creator III
Creator III

Hi Josephine,

There is a step missing, after doing the interval match you have to join the result with the Bookings table, that way you will avoid the Synthetic table.

Carlos M

vishsaggi
Champion III
Champion III

Interval Match creates a Synthetic Keys and it is natural. You should be fine having that, and is allowed.

Read here the last two paragraphs.

IntervalMatch

CarlosAMonroy
Creator III
Creator III

You can try something like:

Bookings:

LOAD BookingsKey,

     [Enrol ID],

     [Course Code],

     [Student ID],

     Gender,

     [Date From],

     [Date To],

     FlagGL,

     School

FROM

[https://community.qlik.com/thread/265110]

(html, codepage is 1252, embedded labels, table is @1);

//Get Max and Min date to create a table with date range

MaxMinDate:

LOAD

  Min(num([Date From],'###0')) as MinDate,

  Max(num([Date To],'###0')) as MaxDate

Resident Bookings;

LET vMinDate = Peek('MinDate',-1,'MaxMinDate');

LET vMaxDate = Peek('MaxDate',-1,'MaxMinDate');

DROP Table MaxMinDate;

//Create a date range

Dates_Tmp:

LOAD

  Date($(vMinDate)+(IterNo()-1),'DD/MM/YYYY') as Date

AutoGenerate(1)

While ($(vMinDate)+(IterNo()-1) <= $(vMaxDate));

//Use interval match to create entry for each date

BookingsTmp2:

IntervalMatch(Date)

LOAD Distinct [Date From], [Date To]  Resident Bookings ; //Important to use Distinct clause other wise the data will be multiplied

DROP Table Dates_Tmp;

//Join Dates to Fact table

Left Join(BookingsTmp2)

LOAD *

Resident Bookings;

DROP Table Bookings;

NoConcatenate

Fact:

LOAD

*

Resident BookingsTmp2

Order by Date;

DROP Table BookingsTmp2;

Anonymous
Not applicable
Author

Hmm, I tried the left join but it dramatically increased the number of rows in the bookings table and didn't display the right values in the pivot table. I think I will leave the synthetic key in there for now after reading Henric's post on the topic. Thanks so much for your help everyone, I was really struggling with this one!